1

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>

Jagadesh
  • 6,489
  • 8
  • 29
  • 30
  • 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 Jun 16 '10 at 17:47
  • Shouldn't you consider deleting this question since you asked a slight variation of the same question shortly after? (See http://stackoverflow.com/questions/3042592/how-to-insert-a-xml-node-as-first-child-in-another-xml-document-in-java.) – Derek Mahar Jun 16 '10 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 Jun 17 '10 at 05:43

1 Answers1

1

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820