Compilable and runnable:
public class JDomQuestion
public static void main(String args)
JDomQuestion jDomQuestion = new JDomQuestion();
private void doit()
Document fakeDocumentWithARoot = createFakeDocumentWithARoot();
Document emptyFakeDocument = createEmptyFakeDocument();
private void transferRootToEmptyDocument(Document fakeDocumentWithARoot, Document emptyFakeDocument)
Element rootElement = fakeDocumentWithARoot.getRootElement();
emptyFakeDocument.setRootElement(rootElement); // Boom ! Here is your exact error.
private Document createEmptyFakeDocument()
return new Document();
private Document createFakeDocumentWithARoot()
Document fakeDocument = new Document();
fakeDocument.setRootElement(new Element("My_name_is_Ozymandias_Root_of_Roots" +
See I can do this. This is not the problem. My problem is transferring from one document to another WITHOUT transferring all the elements and the hierarchy.
So it would be
Fake Document with Root -> Part of the fake document with Root
That's why I thought detach and removecontent could be used to take out those elements that I don't want in my new document
This program tests that hypothesis and strongly indicates the hypothesis is correct by reproducing your exception . In fact, it isolates the specific error you were making.
That's the power of little tiny programs, which are a form experimentation or hypothesis testing or more generally, abstraction.
Ted Gress wrote:*That's* the part I'm having trouble with. I tried the detach and removecontent methods without much luck.
OK I see. What happens when you try to detach it? Do you get an error (other than the one you get when you try to attach it to the new tree..)?
Depending on how the API you're using (JDoms I mean) maybe they won't let you detach the root. Who knows? If you can't detach it somehow, then that means it's still attached , yes? If that's the case then you can't attach it (to the new tree (we believe that's what the error yo're getting is telling you) and therefore you can't move forward doing this.
In that case I think you're going to have to use a copy constructor on a per node basis to transfer the information from one tree to the next. This would be a whole tree iteration.
This is not that radical a notion; trees are stored (serialized) as XML and then turned back into trees at runtime by reading the xml the tree was written to and creating all new nodes from that information.
But before you go there, spend some time nosing around inside the API to see if there's a single method call to do just what you're trying to do (tree. clone()) or node.copy() or util.Transfer(node1,node2) or something like this.
Besides, what DOES detach do if not, you know, detach??
But right now- the first part of my question, is there an error message invovled?
If detach is not working for you then I saw something in the javadoc that says subclasses have to called super.detach() in their detach method.
This is my input document:
This is my output document:
The following is supposed to be "pruned" from the tree. That is, it isn't supposed to be added to the output document and is my main problem:
I checked the code and it should only be adding the ones specified, not the Daemon equipment.
Also, the hierarchy is messed up. Its definitely not adding the nodes one by one, but it has something
to do with the dependencies in the output tree. That is, its adding an element and that element is attached
to other elements. That's why I tried detach() and removecontent().
There are no specific exceptions/errors in the code. Do you need me to show you anything else? Anybody have any ideas
why detach() and removecontent() might not work?
In your OP you had an mystery Exception. Then in the recent post you said:
Ted Gress wrote:But it appears that the node is not being detached or the content is not being removed.
So if your board position has been advanced, let me know so I am addressing the current issue
Actually, I can't speak for anyone else but my impuilse would be, since the Exception I opened with in the first post is no longer being thrown , then this is a new question.
The reason I suggest that is so people in the future, looking for answers to their specific questions, can recognize their questions being asked in a combination of the title of your post + your first question.
Basically, one problem down and now onto another, different problem, albeit in the same code. I know you still feel like you haven't had your problem solved, but you've had one of the problems in your code solved. Now you know you have others . *Sigh*. This is our lot as programmers ...LOL....
Do you think it might help if this were a new, fresh thread so all participants can sync and focus clearly on the new issue?
What I hear you saying is: despite calling detach, and with no Exception being thrown anymore, one Node which should be being removed is not being removed. And this is happening between two JDom Documents
which are local (to a method ?). You cited one specific Node as being transferred when it's not.
Before I think any more about the problem, I need to know if I understand the problem correctly or not.
I'm doing this now by loading the xml tree and only transferring the elements that I don't want pruned from the tree to the new tree for output. The problem is that it isn't "pruning" correctly, some of the elements from the first tree are getting into the second (output) tree when they shouldn't be.