Since I didn't get any answer for this e-mail, I'm sending it again, just in case.
Hope someone can help me on this.
Regards
Alain
----- Message d'origine -----
De: Herten <[EMAIL PROTECTED]>
Date: Wed, 21 Jul 2004 15:09:09 +0200
Sujet: Adding the same Element instance multiple times
�: [EMAIL PROTECTED]
Hi,
Using dom4j 1.4, I've made a little convenient class to parse and update XML
documents, which eventually would be written to a file. While testing it, I happened
on a very strange behaviour at first glance :
I created a simple XML document through the DocumentHelper class, with some Elements
and Attributes in it. Then I tried to add a new "" Element instance 4 times at several
locations of the Document through XPath, which works fine : the corresponding XML file
is updated correctly, and the String representation of the Document (through
OutputFormat, StringWriter and XMLWriter) is correct.
Now when I search all Elements (through document.selectNodes("//test-new")) inside the
Document object in memory (i.e. without re-parsing from the corresponding file !) :
only one Element is returned in the List, with path "/test-new", which is wrong by the
way (I didn't replace the root Element with ). And the 3 other elements are simply
ignored !
After some more tests I figured out that adding the same Element instance to the
document at several locations wasn't the best solution. Apparently I need to clone()
that instance every time I want to add it. OR, the other solution was to re-parse the
Document from the XML file (after I had inserted the same Element several times in the
Document and wrote it to the file), so that a new Document tree was created again, and
the selectNodes() method would return the 4 Elements correctly. But this would cost
too much in performance of course...
My questions are : is there something wrong with my experiments and conclusions above ?
Is there another way to add the same Element instance several times inside a document
without having to clone it every time ?
Thanks for your help. I hope everything was clear in my explanations.
Regards,
Alain
-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_idP47&alloc_id808&op=click
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user