[Logo]
Forums Register Login
Does anybody have experience with JDOM?
 

Compilable and runnable:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import org.jdom2.Document;
import org.jdom2.Element;
 
 
public class JDomQuestion
{
    public static void main(String[] args)
    {
        JDomQuestion jDomQuestion = new JDomQuestion();
        jDomQuestion.doit();
    }
 
    private void doit()
    {
        Document fakeDocumentWithARoot = createFakeDocumentWithARoot();
        Document emptyFakeDocument = createEmptyFakeDocument();
 
        transferRootToEmptyDocument(fakeDocumentWithARoot, emptyFakeDocument);
    }
 
    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" +
                                              "Look_on_my_bug_ye_Mighty_and_despair."));
 
        return fakeDocument;
    }
}




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
You had an error. That error was because the node  was already attached to a document. That's what your exception was telling you and that was the issue; your mistake was failing to detach the node from the previous tree (document). That was what I suggested in my first post way up there.

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.

HTH
This tiny program would be a perfect example of what I need except for the following. The "fakeDocumentWithoutRoot" that is transferred over to the "emptyfakedocument" can't copy over the exact same JDOM tree. Basically, I need to be able to "prune" the "fakeDocumentWithoutRoot" before sending it over to "emptyfakedocument". *That's* the part I'm having trouble with. I tried the detach and removecontent methods without much luck. Can you give me a hand? Should I be going in that direction?
 

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?
This works for what you're trying to do. Notice the successful call to detach() and the subsequent use of the detached root node as the root of a new tree.

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.

No, no error with detachment or removing content.

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?

-Ted

OK so this is new news. Your original problem which was producing this error has gone away, correct?

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.
 

Ted Gress wrote:? Anybody have any ideas
why detach() and removecontent() might not work?



I doubt this is your problem. The methods seem to work.
I need to be able to take the loaded xml tree, essentially prune it (remove some elements that aren't valid), and transfer it to a new tree for output.

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.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 915 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jun 23, 2018 04:12:42.