Dashboard > Public Content > ... > XMLBits > Copy XML Nodes From One Document To Another
  Public Content Log In   View a printable version of the current page.  
  Copy XML Nodes From One Document To Another
Added by James Richardson, last edited by James Richardson on Jul 27, 2006
Labels: 
(None)

You need to import the nodes first...

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();

            Element root = document.createElement("rootnode");
            document.appendChild(root);

  String xml = resourceLoader.loadString(target);
                        Document otherDocument= XMLUtil.parseXML(xml);

                        NodeList nodelist = otherDocument.getElementsByTagName("nodestocopy");

                        if (nodelist.getLength() == 0) {
                            throw new Exception("Resource " + target + " doesn't contain any nodestocopy");
                        }

                        for (int n = 0; n < nodelist.getLength(); n++) {
                            Node node = nodelist.item(n);
                            Node nodeToImport = document.importNode(node, true);
                            root.appendChild(nodeToImport);
                        }

XMLUtil class

public class XMLUtil {
    public static Document parseXML(String xml) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setExpandEntityReferences(false);
        factory.setValidating(false);
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        documentBuilder.setErrorHandler(new ErrorHandler() {
            public void error(SAXParseException exception) throws SAXException {
                throw new RuntimeException(exception.toString());
            }

            public void fatalError(SAXParseException exception) throws SAXException {
                throw new RuntimeException(exception.toString());
            }

            public void warning(SAXParseException exception) throws SAXException {
                System.out.println(exception.getMessage());
            }
        });

        return documentBuilder.parse(new InputSource(new StringReader(xml)));
    }

    public static String toString(Document document) throws TransformerException {
        Transformer t = newIndentingTransformer();
        StringWriter resultWriter = new StringWriter();
        Result result = new StreamResult(resultWriter);
        t.transform(new DOMSource(document), result);
        return resultWriter.toString();
    }

    public static String toString(Node node) throws TransformerException {
        Transformer t = newIndentingTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter resultWriter = new StringWriter();
        Result result = new StreamResult(resultWriter);
        t.transform(new DOMSource(node), result);
        return resultWriter.toString();
    }

    private static Transformer newIndentingTransformer() throws TransformerConfigurationException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
        return t;
    }

}
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators