Hi,

I have the following xml that I want to manipulate with dom4j:

<web-app>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
      org.apache.struts.action.ActionServlet
    </servlet-class>

*** want to insert new init param here ***

    <init-param>
      <param-name>application</param-name>
      <param-value>ApplicationResources</param-value>
    </init-param>


I'd like to insert a new init param immediately after the servlet-class element. I have a solution which is very ugly


1) locate servlet element using xpath
XPath xpathSelector =
DocumentHelper.createXPath("/web-app/servlet/servlet-name[text()='action']/parent::*");

2) iterate over the elements counting the index of the servlet-class element

3) construct ArrayList of elements from original elements

4) add <init-param> element to servlet element (this adds it as the last child servlet element)

5) add this new element into the ArrayList after the servlet-class element.

6) set the contents of the servlet element with the modified ArrayList

This feels like a hack. It feels to me that there should be some other way of achieving this outcome.

Is there any other way to create an element without adding it as the last child of the parent it was created on?

Is there any way to add an element as a sibling, after a specific element?

cheers
Nathan

--
Nathan Coast
Managing Director
codeczar ltd
mobile: (852) 9049 5581
email:  mailto:[EMAIL PROTECTED]
web:    http://www.codeczar.com


------------------------------------------------------- This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more http://productguide.itmanagersjournal.com/guidepromo.tmpl _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to