Hi,

I still don't get it. If one extracts a list of nodes into a Java List by an
XPath query, and then adds a node into that List, the Node doesn't get added
into the original document from which the list was queried (makes sense
here). The only way to do it is to use the .appenChild() or .insertBefore()
etc. methods of the Document or Element. But I also observe this:

When I create a new Element and try to add it to a Document as:
document.insertBefore(newChild, refChild), the newChild is always inserted
right at the BEGINNING of the document, BEFORE the document element, hence
making TWO Root elements!!! (Irrespective of refChild). First of all, why is
the function inserting it at the top instead of before the refChild element?
Secondly, why is the function ALLOWING this insert at an illegal position -
because, when I try to open the resultant XML, it obviously gives the error
that there cannot be more than one Root element!

Is this again an issue with org.w3c.dom as opposed to org.dom4j.dom? 

Here's the code sample:

import org.w3c.dom.*;
import org.dom4j.dom.*;
import java.util.*;
//import etc...
//...

DOMDocument oDocument = new DOMDocument("Root");
//Add a Root element    
Element oParent = oDocument.createElement("Parent");
oParent.setAttribute("id", "P01");
//Add a child element
oDocument.appendChild(oParent);
Element oChild = oDocument.createElement("Child");
oChild.setAttribute("id", "C01");
oParent.appendChild(oChild).appendChild(oChild.cloneNode(false));
//Get the Child Element in a list
List oNodes = oDocument.selectNodes("//Child[@id]"); //Will retrieve the one
single Child Element
Element oGuest = oDocument.createElement("Guest");
oGuest.setAttribute("name", "newcomer");
oNodes.add(0, oGuest); //Doesn't affect the original document
oDocument.insertBefore(oGuest, (Node)oDocument.selectSingleNode("//Child"));
System.out.println(oDocument.asXML());

Here's the Output:
<Guest name="newcomer"/>
<Parent id="P01">
        <Child id="C01"/>
</Parent>

I doubt this is what .insertBefore() is supposed to do. Have I missed
something here...? An explanation would be appreciated... (Here's another
one for you James :-).

Thanks 
Soumanjoy


-----Original Message-----
From: James Strachan [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, April 23, 2002 8:10 PM
To: Christian Holmqvist, IT, Posten; [EMAIL PROTECTED]
Subject: Re: [dom4j-user] Add element between elements in a document.


A Branch (a Document or Element) has a method called content() which returns
a modifiable List. So you can use the standard Java 2 List interface to
change the content of an Element or Document.

e.g.

DocumentFactory factory = new DocumentFactory();
Document doc = factory.createDocument();
Element root = doc.addElement( "html" );
Element header = root.addElement( "header" );
Element footer = root.addElement( "footer" );

// now lets add <foo> in between header & footer
List list = root.content();
Element foo = factory.createElement( "foo" );
list.add( 1, foo );

// assertions
assertTrue( list.size() == 3 );
assertTrue( list.get(0) == header );
assertTrue( list.get(1) == foo );
assertTrue( list.get(2) == footer );


The above code has all been added to the unit test
dom4j/src/test/org/dom4j/TestContent in the method testAddingInTheMiddle().

James
----- Original Message -----
From: "Christian Holmqvist, IT, Posten" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, April 23, 2002 2:32 PM
Subject: [dom4j-user] Add element between elements in a document.


> Hi
>
> I have a small problem with adding elements.
>
> I got a document that looks something like this:
>
> Doc
>  |
>  --- Header
>  |
>  |
>  --- Footer
>
> On the document handler class (my class) there is a method called add
> element. This element should be placed in between the header and the
footer
> i.e:
>
> Doc
>  |
>  --- Header
>  |
>  -> (new element)
>  |
>  --- Footer
>
> My problem is that I can not figure out how to add a elemnt to a specific
> position in a document.
>
> Help... please...
>
> Cheers Christian Holmqvist
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to