I have a xml document like this rootXMLDoc=<root> <param></param></root> . I need to insert paramxmlDoc= <parameter par='1'>abc</parameter>. how to insert paramxmlDoc to rootXMLDoc in java.? and i need output like this
<root>
<parameter par='1'>abc</parameter>
<param></param>
</root>
-
Which Java XML parser/generator library do you use? There are many such libraries, so the answer depends on the library that you use. For example do you use a DOM or SAX XML library?Derek Mahar– Derek Mahar2010-06-16 17:47:48 +00:00Commented Jun 16, 2010 at 17:47
-
Shouldn't you consider deleting this question since you asked a slight variation of the same question shortly after? (See stackoverflow.com/questions/3042592/…)Derek Mahar– Derek Mahar2010-06-16 17:59:28 +00:00Commented Jun 16, 2010 at 17:59
-
But the answers for both of my similar questions are different. So I didnt consider to delete this one. May be it ll be useful for someone in future.Jagadesh– Jagadesh2010-06-17 05:43:07 +00:00Commented Jun 17, 2010 at 5:43
Add a comment
|
1 Answer
Like this:
Element e = paramxmlDoc.getRootElement();
paramxmlDoc.setRootElement(null); // break connection between doc and element
rootXMLDoc.getRootElement().addChild(e); // Insert node in other document
Note: This is from memory, so the actual method calls can be slightly different but you get the idea.