2

My XML looks like this.

I would like to "export" collected_objects into another document. Here is my code-

  NodeList nList = reader.getElementsByTagName("collected_objects");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                output.importNode(nNode, true);

            }

output refers to the new document I want to write to.

The code is not importing anything from source document. All I get is XML "header" - <?xml version="1.0" encoding="UTF-8" standalone="no"?>

I was expecting that since I've set deep to true, all of the child nodes will be imported but that is not happening.

What am I doing wrong?

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
user837208
  • 2,487
  • 7
  • 37
  • 54

2 Answers2

7

importNode only imports the node to the document. You still have to append it somewhere using Node.appendNode(child)

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
3

use importNode using this way

Element rootElement = doc.getElementsByTagName("collected_objects");
doc.appendChild(rootElement);
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {               
        rootElement.appendChild(doc.importNode(n, true));
}
Riddhish.Chaudhari
  • 833
  • 1
  • 8
  • 24