简体   繁体   中英

How do I add element from an existing document to a new document?

For my case, I need to take some nodes from the existing XML file and create a new document and add the all nodes that are in existing file.

How do I add element from an existing document to a new document?

Likely problem is that you tried to insert node to the target document without importing it. Importing node to target document is done via Document.importNode . Code is roughly as follows, of course exact location in target document should be changed specific to you application:

    NodeList list ...
    for(int i=0; i < list.getLength(); i++){
        Node nodeToImport = list.item(i);
        Node importedNode = targetDocument.importNode(nodeToImport, true);
        targetDocument.getDocumentElement().appendChild(importedNode);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM