简体   繁体   中英

How to add nodes from another xml using xmlbeans

I am using xmlbeans to generate the xml document, while I need to extract all the children from another xml file and insert them to my current document. The to_be_add.xml :

<root>
    <style>
        .....
    </style>
    <atlas img="styles/jmap.png">
        ....
    </atlas>
    .....
</root>

And this xml file does not have a schema so I do not create related java class to map it. You think it as a plain xml file.

I want the style atlas node added. I use the following codes:

        XmlObject pointRoot = XmlObject.Factory.parse(Main.class.getResourceAsStream("to_be_added.xml"));
        NodeList nodeList = pointRoot.getDomNode().getChildNodes();

        Node themeNode = renderthemeDoc.getDomNode();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            themeNode.appendChild(node);
        }

Then I got error:

Exception in thread "main" org.apache.xmlbeans.impl.store.DomImpl$WrongDocumentErr: Child to add is from another document

And I found this post by searching "child to .... another document": how to add a xml document to another xml document in java which said that the connection between the element and the document has to be broken between the element can be add to other document.

So I try to build the Document object(that is why the variable pointDoc and themeDoc exist):

        XmlObject pointRoot = XmlObject.Factory.parse(Main.class.getResourceAsStream("to_be_added.xml"));
        Document pointDoc = pointRoot.getDomNode().getOwnerDocument();
        System.out.println(pointDoc);
        Element element = pointDoc.getDocumentElement();
        NodeList nodeList = element.getChildNodes();

        Document themeDoc = myCurrentDoc.getDomNode().getOwnerDocument();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            node = themeDoc.importNode(node, true);
            themeDoc.appendChild(node);
        }

Then I got NullPointerException which said that the pointDoc is null.

That is the whole process how I try to solve this problem. If it is unclear, please tell me, I will update accordingly.

Is it possible to fix it?

Since your other XML file is not mapped to a class, you can use a regular DOM parser to read it and extract its nodes. But using a generic object factory you can still get the nodes:

    XmlObject pointRoot = XmlObject.Factory.parse(  "<root>\n" +
                                                    "    <style>\n" +
                                                    "    </style>\n" +
                                                    "    <atlas img=\"styles/jmap.png\">\n" +
                                                    "    </atlas>\n" +
                                                    "</root>");

    Node pointDoc = pointRoot.getDomNode().getFirstChild();
    NodeList nodeList = pointDoc.getChildNodes();

    for(int i = 0; i < nodeList.getLength(); i++) {
        System.out.println("Node: " + nodeList.item(i).getNodeName());
    }

This will print:

Node: #text
Node: style
Node: #text
Node: atlas
Node: #text

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