3

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?

2
  • Your question is not clear. Please describe your problem in detail, including a larger portion of your XML documents and Java files (including your XML beans classes). Commented Jun 15, 2014 at 5:12
  • got this working under XML Beans 2.4.0 using Document themeDoc = (Document) myCurrentDoc.getDomNode(); Commented May 31, 2017 at 12:38

1 Answer 1

-2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your attention, but I am afraid you misunderstand me. I can read the nodes from another xml, but I have no idea to add them to my current document.
You said you couldn't: "However the pointDoc is null. Is it possible to fix it?" What do you mean by that then? If the node is null you couldn't extract the child nodes, that's what I understood. The code above shows how to fix that.
Why I want to get the Document object is that I try to break the nodes and the document of the "to_be_added.xml", since the nodes can not added if they are still have connection with another document. Check this:stackoverflow.com/questions/3035953/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.