Hi!
Oki, I don't use the daily build her (at work) so I can't try it out.
I totally agree with you in all cases and we agree upon the result so If the
result is not what it should be there is a problem in insertBefore.
When a element is created based on a document i.e. in your example
oDocument.createElement() it contains a parent and therefor implicit also a
place in the document. Exactly what this place is I can't tell without
reading the src code and see how it is handled.
A simple test would be to create a document with a root element, do
createElement and then without appending the created element to the document
print the document.
If the element shows up the element already got a position in the document
otherwise this is handled in some other fashion.
If instead a element is created with DocumentHandler.createElement(..) witch
is a static method just creating a new Element without a parent (i.e. parent
== null) there is no connection to the document.
Hmm I think that was all...
Cheers Christian
> Hi Christian,
>
> First of all, I must apologize and clear some of the mistakes
> in my previous
> mail:
> I had first tried with the line:
>
> oParent.appendChild(oChild).appendChild(oChild.cloneNode(false));
>
> and then with simply:
>
> oParent.appendChild(oChild);
>
> Which would result in only one single Child node. Moreover, I
> had tried the
> code with the line
>
> oNodes.add(0, oGuest);
>
> and without it, and
>
> oDocument.insertBefore(oGuest,
> (Node)oDocument.selectSingleNode("//Child"));
>
> I have tried these two statement separately and printed the
> document with
> System.out.println(oDocument.asXML());
>
> In ALL cases, I am getting the Guest element placed right at
> the top level.
> I didn't understand the last part of your explanation: I DID
> say that the
> tree structure that you drew
>
> parent
> |
> -guest
> |
> -child
> |
> -child
>
> was SUPPOSED to be, had the insertBefore() method worked the
> way it should
> have (that is where we both agree). But it doesn't turn out
> that way. And I
> need to be clear about one thing: after an element has been
> newly created
> with Document.createElement(), and BEFORE it has been
> appended to a parent
> Node, does it already have a postion in the XML Document
> (according to what
> you say?). I don't get any errors, even in runtime - the
> whole code runs and
> gives the output as I have described.
>
> As for the compilation error, there have been some changes in
> the build in
> the last three days, so you'll have to take the daily build
> in order for the
> code to work.
>
> And for your question: why I'm using DOMDocument - no
> particular reason. I
> know there are probably 10 different ways to achieve the same
> thing, but I
> just wanted to explore many possibilities before choosing one
> of the ways
> for my next project. And I wanted especially to explore the
> w3c.dom classes
> because they conform with the MS-DOM structure, that's all.
>
> If you don't mind, I would appreciate some further
> clarification on this.
> Thanks
> Soumanjoy
>
>
> -----Original Message-----
> From: Christian Holmqvist, IT, Posten
> [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, April 25, 2002 12:48 PM
> To: 'Soumanjoy Das'; 'James Strachan';
> [EMAIL PROTECTED]
> Subject: RE: [dom4j-user] Add element between elements in a document.
>
>
> Hi!
>
> First of all, I could not even run your example, got the error:
> java.lang.ClassCastException: org.dom4j.tree.DefaultElement
> at org.dom4j.dom.DOMDocument.createElement(DOMDocument.java:209)
> at
> se.posten.logistik.applications.cr.test.DOM4J_test.<init>(DOM4
> J_test.java:29
> )
> at
> se.posten.logistik.applications.cr.test.DOM4J_test.main(DOM4J_
> test.java:90)
>
> But anyway.
> If I understand the this correct does this:
> oParent.appendChild(oChild).appendChild(oChild.cloneNode(false));
> add two child elements like this.
> parent
> |
> -child
> |
> -child
>
> The list from this command will be of size two containing two
> child elements
> > List oNodes = oDocument.selectNodes("//Child[@id]"); //Will
> > retrieve the one
> > single Child Element
> These are
> child
> |
> -child
> AND
> child
>
> The next statement of value is
> > oNodes.add(0, oGuest); //Doesn't affect the original document
> Where in the documentation do you find that this is not
> references to the
> original document. At least I can not find any cloning or
> detachment in the
> doc or code...
>
> But the question is, where does the guest element go.
> Intutinally I would
> say, right before the first child element in the original document,
> resulting in a document looking like this:
> parent
> |
> -guest
> |
> -child
> |
> -child
>
> and the ofcourse first in the node list.
>
> BUT then this:
> > oDocument.insertBefore(oGuest,
> (Node)oDocument.selectSingleNode("//Child"));
> should not have been allowed because the guest element
> already got a parent
> and a place in the document.
> But on the other hand.
> The guest element should be placed before the first child
> element witch is
> where the parent element is. And when using this in contrast
> to that the
> node list do indeed affect the org document and there is the
> guest placed at
> position 0 then the result would be that the document looks like your
> result.
> I would say that the document should look like:
> parent
> |
> -guest
> |
> -child
> |
> -child
> if it where correct. Don't you think so?
>
> Another question, why do you use the DOMDocument and not the
> DocumentHelper
> and org.Dom4j.Element and Document?
>
> Cheers Christian Holmqvist
>
> > -----Ursprungligt meddelande-----
> > Fr�n: Soumanjoy Das [mailto:[EMAIL PROTECTED]]
> > Skickat: den 25 april 2002 06:58
> > Till: 'James Strachan'; [EMAIL PROTECTED]
> > �mne: RE: [dom4j-user] Add element between elements in a document.
> >
> >
> > 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
> >
>
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user