// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.io.StringReader;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class XmlMerge {
    public static Document load(String s) throws JDOMException, IOException {
        SAXBuilder b = new SAXBuilder();
        return b.build(new StringReader(s));
    }
    public static void print(Document d) {
        XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
        System.out.println(fmt.outputString(d));
    }
    public static Document merge(Document d1, Document d2) throws JDOMException {
        Document doc = new Document();
        Element delm = new Element("d");
        doc.addContent(delm);
        delm.addContent(d1.getRootElement().cloneContent());
        delm.addContent(d2.getRootElement().cloneContent());
        return doc;
    }
    public static void main(String[] args) throws JDOMException, IOException {
        Document d1 = load("<d><e>a</e><e>bb</e></d>");
        print(d1);
        Document d2 = load("<d><e>ccc</e><e>dddd</e></d>");
        print(d2);
        Document d3 = merge(d1, d2);
        print(d3);
    }
}
