/**
*
*/
package de.tutorials;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/**
* @author Tom
*
*/
public class MergeXmlDocumentsExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
String xml1 = "<system><classes></classes></system>";
String xml2 = "<root><target></target></root>";
Document mainDocument = documentBuilder.parse(new InputSource(
new StringReader(xml1)));
Document document = documentBuilder.parse(new InputSource(
new StringReader(xml2)));
mainDocument.getFirstChild().appendChild(
mainDocument.importNode(document.getFirstChild(), true));
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource(mainDocument), new StreamResult(System.out));
}
}