Maarten, I can't come-up with a test case that fails. This is close to what I'm doing and it works:
Document doc = null; try { SAXReader reader = new SAXReader(); doc = (DefaultDocument)reader.read(new File("test.xml")); } catch (Exception e) { e.printStackTrace(); } try { Element root = (Element)doc.selectSingleNode("/root"); Element parent = (Element)doc.selectSingleNode("/root/parent"); Node child = doc.selectSingleNode("/root/parent/child"); System.out.println("root : " + root.getUniquePath()); System.out.println("parent : " + parent.getUniquePath()); System.out.println("child : " + child.getUniquePath()); Node clone1 = (Node)child.selectSingleNode("ancestor-or-self::child").clone(); ((Element)child.selectSingleNode("ancestor-or-self::parent")).add(clone1); Node clone2 = (Node) clone1.clone(); root.addElement("parent"); ((Element)clone1.selectSingleNode("ancestor-or-self::parent")).add(clone2); if (clone1.getDocument() == null) System.out.println("Clone1 has no document"); if (clone1.getParent() == null) System.out.println("Clone1 has no Parent"); if (clone2.getDocument() == null) System.out.println("Clone2 has no document"); if (clone2.getParent() == null) System.out.println("Clone2 has no Parent"); System.out.println("Child : " + child.getUniquePath()); System.out.println("Clone1 : " + clone1.getUniquePath()); System.out.println("Clone2 : " + clone2.getUniquePath()); } catch (Exception e) { e.printStackTrace(); } Where test.xml = <root><parent><child></child></parent></root> The ancestor search for the node's true parent (per the schema) is much more complex than what I have here, I'm thinking that maybe the source of the problem. Thanks for looking at this, for now consider it an application problem. --Mike --- Maarten Coene <[EMAIL PROTECTED]> wrote: > Mike, > > I couldn't reproduce your problem with a simple junit test: > > public void testAddCloneToOtherElement() { > DocumentFactory factory = DocumentFactory.getInstance(); > Document doc = factory.createDocument(); > Element root = doc.addElement("root"); > Element parent1 = root.addElement("parent"); > Element child1 = parent1.addElement("child"); > > Element parent2 = (Element) parent1.clone(); > root.add(parent2); > > assertSame("parent not correct", root, parent2.getParent()); > assertSame("document not correct", doc, parent2.getDocument()); > > Element child2 = parent2.element("child"); > > assertNotSame("child not cloned", child1, child2); > assertSame("parent not correct", parent2, child2.getParent()); > assertSame("document not correct", doc, child2.getDocument()); > } > > Could you tell me what you are doing different and if possible, could > you change the junit test so that it fails? > > thanks, > Maarten > > Mike Summers wrote: > > >I have a dom4j program that adds cloned nodes into a Document. > > > >I'm noticing that when it comes time to process the cloned node (this can > >include cloning the clone) (I use a recursive treewalk) that the clone is > >always missing a Document, and frequently a Parent. This only happens when I > >process a clone. > > > >*Roughly*, the scheme is: > > > >private void processNode(Node node){ > > clone = node.clone(); //createCopy has the same effect > > try{ > > findYourParentXPath.selectSingleNode(node).add(clone); > > }catch(NullPointerException npe){ > > node.getParent().add(clone); > > } > > treewalk(clone); > >} > > > >with treewalk eventually calling back into processNode. > > > >Dumping the Document I get the mis-processed clones, although in the wrong > >location (the search for their parent fails due to the null Document). > > > >Is this out-of-scope for dom4j or am I missing some step? It's almost like > the > >tree needs to be re-read before processing the clone (not an option in this > >case). > > > >If someone has an example of cloning a clone and inserting it into the > Document > >I'd appreciate a look. > > > >Thanks-- Mike > > > > > >------------------------------------------------------- > >This SF.Net email is sponsored by: > >Sybase ASE Linux Express Edition - download now for FREE > >LinuxWorld Reader's Choice Award Winner for best database on Linux. > >http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click > >_______________________________________________ > >dom4j-user mailing list > >[EMAIL PROTECTED] > >https://lists.sourceforge.net/lists/listinfo/dom4j-user > > > > > > > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: > Sybase ASE Linux Express Edition - download now for FREE > LinuxWorld Reader's Choice Award Winner for best database on Linux. > http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click > _______________________________________________ > dom4j-user mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/dom4j-user > ------------------------------------------------------- This SF.Net email is sponsored by: Sybase ASE Linux Express Edition - download now for FREE LinuxWorld Reader's Choice Award Winner for best database on Linux. http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user