Hi,

I am new to dom4j and wanted to see if was a better way to insert new
elements than I have coded.  The insert example uses cloning, but cloning
is not an option for me as I may not have a suitable element laying around
to clone.  When I try to construct an Element, I am told I cannot.  so I
end up adding the element which will place it in an uncontrollable index
position, detaching it, an then using the example method to insert.  This
seems cluncky, especially when I usually just want it added as the last
child of the parent element.  Thanks for any ideas of a better way to do
this.  Example below:

      Node nodeToInsertAfter =
            document.selectSingleNode("/RootNode/NodeToInsertAfter");
      int nodeToInsertAfterIndex = root.indexOf(nodeToInsertAfter);
      Element nodeToInsert = root.addElement("NodeToInsert");
      insertElementAt(
            root,
            (Element) nodeToInsert.detach(),
            nodeToInsertAfterIndex + 1);


      public void insertElementAt(
            Element parent,
            Element newElement,
            int index) {
            List list = parent.content();
            list.add(index, newElement);
      }



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to