From: "Mitch Gitman" <[EMAIL PROTECTED]>
> I'm trying to drill down a few levels from the root element of a document
> and then remove an element. I obtain a reference to the element I want to
> remove plus its parent, and I call:
> parentElement.remove(childElement);
>
> I get an org.dom4j.IllegalAddException. What's interesting about the
> exception is that it describes the addition of the root element to a null
> branch. It's like dom4j doesn't know to stop before it gets to this point:
> org.dom4j.IllegalAddException: The node
> "org.dom4j.tree.DefaultElement@8941f7 [Element: <wsdl:definitions uri:
> http://schemas.xmlsoap.org/wsdl/ attributes: [org
> .dom4j.tree.DefaultAttribute@49d67c [Attribute: name targetNamespace value
> "urn:
> other.root"]]/>]" could not be added to the branch "null" because: The
Node alr
> eady has an existing document: org.dom4j.tree.DefaultDocument@b4e29b
> [Document: name file:/C:/ALLDOCS/CODE/GENERAL/output/deploy/idea.wsdl]
>          at
org.dom4j.tree.DefaultDocument.addNode(DefaultDocument.java:253)
>          at org.dom4j.tree.AbstractBranch.add(AbstractBranch.java:221)
>          at
> org.dom4j.tree.AbstractDocument.setRootElement(AbstractDocument.java:


I don't understand how calling parentElement.remove() can invoke the
setRootElement() method on Document. Any chance you could show us a snippet
of the code you're using, or maybe a full stack trace.

>From the stack trace you've provided it looks like you're trying to add an
Element to a Document which is already connected to another Document - hence
the exception.

If you want to remove an Element from one document so that you can add it to
another, use detach().

Document one = ...;

DocumentFactory factory = new DocumentFactory();
Document two = factory.createDocument();

Element element = one.getRootElement();

// remove the root element of one and add it to two
two.setRootElement( element.detach() );

James
-------
http://radio.weblogs.com/0112098/



-------------------------------------------------------
This sf.net email is sponsored by: To learn the basics of securing 
your web site with SSL, click here to get a FREE TRIAL of a Thawte 
Server Certificate: http://www.gothawte.com/rd524.html
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to