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?

Community
  • 1
  • 1
hguser
  • 35,079
  • 54
  • 159
  • 293
  • 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). – helderdarocha Jun 15 '14 at 05:12
  • got this working under XML Beans 2.4.0 using `Document themeDoc = (Document) myCurrentDoc.getDomNode();` – Claude May 31 '17 at 12:38

1 Answers1

-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
helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • 1
    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. – hguser Jun 15 '14 at 05:04
  • 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. – helderdarocha Jun 15 '14 at 05:08
  • 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:http://stackoverflow.com/questions/3035953/how-to-add-a-xml-document-to-another-xml-document-in-java – hguser Jun 15 '14 at 05:37