Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Node into new Document

I'm trying to import a node from a base-document into a new document but when I use the Document.importNode() method, it generates an exception. The funny part is that this exception does not contain a message so I'm pretty much blind except for the stack-trace. (I'm new to java)

Here is my method that will create a new document and import the node:

  • this.Builder = DocumentBuilder
  • this.Element = Node

Code:

public XMLSelection extract ()
{
    if (this.Element != null)
    {
        try
        {
            Document newDoc = this.Builder.newDocument();
            Node node = newDoc.importNode(this.Element, true);

            newDoc.appendChild(node);
            return new XMLSelection(newDoc);
        }
        catch(Exception e)
        {
            Debug.error("XMLSelection::extract", e);
        }
    }
    return null;
}

And the stack-trace:

02-06 14:27:53.328: W/System.err(9082): org.w3c.dom.DOMException
02-06 14:27:53.328: W/System.err(9082):     at org.apache.harmony.xml.dom.NodeImpl.setNameNS(NodeImpl.java:227)
02-06 14:27:53.328: W/System.err(9082):     at org.apache.harmony.xml.dom.ElementImpl.(ElementImpl.java:50)
02-06 14:27:53.328: W/System.err(9082):     at org.apache.harmony.xml.dom.DocumentImpl.createElementNS(DocumentImpl.java:336)
02-06 14:27:53.328: W/System.err(9082):     at org.apache.harmony.xml.dom.DocumentImpl.shallowCopy(DocumentImpl.java:156)
02-06 14:27:53.328: W/System.err(9082):     at org.apache.harmony.xml.dom.DocumentImpl.cloneOrImportNode(DocumentImpl.java:208)
02-06 14:27:53.328: W/System.err(9082):     at org.apache.harmony.xml.dom.DocumentImpl.importNode(DocumentImpl.java:222)
02-06 14:27:53.328: W/System.err(9082):     at com.xxxx.xxxx.XMLSelection.extract(XMLSelection.java:57)
like image 897
Daniel Avatar asked Feb 06 '12 14:02

Daniel


1 Answers

Try a cloneNode followed by an adoptNode to use a child element as the root element of a new Document.

    String xml = "<hello><world /></hello>";
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document document0 = db.parse(new InputSource(new StringReader(xml)));
        Document document1 = db.newDocument();


        NodeList nodeList = document0.getDocumentElement().getChildNodes();
        document1.appendChild(document1.adoptNode(nodeList.item(0).cloneNode(true)));

        // Serialize the two documents to check that the copy was correct.
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document0), new StreamResult(output));
        System.out.println(output.toString("UTF-8"));

        output.reset();
        transformer.transform(new DOMSource(document1), new StreamResult(output));
        System.out.println(output.toString("UTF-8"));
    } catch (TransformerException e) {
    } catch (IOException e) {
    } catch (SAXException e) {
    } catch (ParserConfigurationException e) {
    }
like image 183
Jens Avatar answered Nov 04 '22 23:11

Jens