Naveen & Michael, thanks. The a1.CreateCopy did the trick...
If I try to add the element to the root of another document then I get and IllegalAddException. Saying the the original node already has a parent. As usual, it all makes sense once it works! Thanks again, Marty -----Original Message----- From: Naveen Murthy [mailto:[EMAIL PROTECTED] Sent: Friday, January 09, 2004 10:11 AM To: Bradley, Martin Subject: RE: [dom4j-user] Create an Document from fragments of another Marty, ive been out of dom4j prgramming for a while.. Im sure ive done this before. But i vaguely remember the syntax ..TRY this here is the steps: 1. get a reference to the a1 element Element a1 = origDOM.element("a1"); 2. create a copy of a1 and add it to new DOM newDOM.getRoot().addElement(a1.createCopy() ) ; i think this should do the trick. thx, Naveen -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Bradley, Martin Sent: Friday, January 09, 2004 10:00 AM To: [EMAIL PROTECTED] Subject: [dom4j-user] Create an Document from fragments of another Hi all, I seem to be stumped on this one. I haven't been using the dom4j api long and I'm having trouble pulling a node set our of one document and then using it to create another. for example I have the following xml: <aaa> <a1> <bbb /> <ccc /> </a1> <a1> <bbb /> <ccc /> </a1> </aaa> I want to pick up the first a1 and create a document as follows: <onlyone> <a1> <bbb /> <ccc /> </a1> </onlyone> What I get is: <onlyone> <bbb /> <ccc /> </onlyone> Here's the code (removed try...catch for brevity) SAXReader reader = new SAXReader(); Document inDoc = reader.read(f); List selectedList = inDoc.selectNodes("aaa/a1"); Iterator suspects = selectedList.iterator(); for (;suspects.hasNext();) { Branch treeFragment = (Branch)suspects.next(); Document document = DocumentHelper.createDocument(); Element root = DocumentHelper.createElement("suspect"); document.setRootElement(root); root.appendContent(treeFragment); System.out.println(document.asXML()); } Any ideas? Am i missing something obvious? Thanks for any help, Marty NOTICE: This E-mail may contain confidential information. If you are not the addressee or the intended recipient please do not read this E-mail and please immediately delete this e-mail message and any attachments from your workstation or network mail system. If you are the addressee or the intended recipient and you save or print a copy of this E-mail, please place it in an appropriate file, depending on whether confidential information is contained in the message. ?+sSXs'Su=ߢJ?~Sz+-...ZԨ~ਝS?jvzg,}Sxvq[?)qZn)b'jwZ?qZz~t(tm)Z+sŭ"?izr?ߢZvsfms_&?j)bz bh>^ǫXy+zm>b<q-+-bا~ݢn# ------------------------------------------------------- This SF.net email is sponsored by: Perforce Software. Perforce is the Fast Software Configuration Management System offering advanced branching capabilities and atomic changes on 50+ platforms. Free Eval! http://www.perforce.com/perforce/loadprog.html _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user