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 "<test-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 <test-new> 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 <test-new>). And the 3 other 
<test-new> 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 <test-new> 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 <test-new> 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_id=4721&alloc_id=10040&op=click
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to