• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does anybody have experience with JDOM?  RSS feed

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm having some trouble with JDOM2. I want to serialize my tree and I go to add the first root element and it says its already added to the document, but it's a new document I'm adding it to.

Code:




And my stack trace:

 
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't that be



?
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure. Can you expand?


Do you mean this:

should be this:



??
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:... I go to add the first root element...



But an XML document can only have one root element. That's why I was expecting that you would be wanting to add the subsequent data as children of the root element, rather than adding them to the document (which can't be done, as the error message says).
 
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:Hi,

I'm having some trouble with JDOM2. I want to serialize my tree and I go to add the first root element and it says its already added to the document, but it's a new document I'm adding it to.

Code:




And my stack trace:



What the error is telling you is the element you tried to add to the root ALREADY is the child of some root. The error line numbers don't correspond to the line umbers on your post (obviously) and the element it refers to is not in yuour code and your code is just a snippet with custom objects we don't know the definition to so it's hard to say more.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:Hi,

I'm having some trouble with JDOM2. I want to serialize my tree and I go to add the first root element and it says its already added to the document, but it's a new document I'm adding it to.

Code:




And my stack trace:



LOL my useful reply in the previous post got caught within the code element of your post. It appears as line 16.. lol..

is there some way to re-edit a post you've made?
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, I've edited your first reply.
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify, Ted: you have a SceneGraphManager object which represents a tree structure, and you want to duplicate that structure (and its contents) as a JDOM tree. Is that what you meant by "serialize my tree"?
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Ok. To clarify I have a SceneGraph class which contains a tree of SceneGraph Nodes. Each node has a xmlData attribute, which is of the Element class.

When I mean serialize, I want to walk through that tree and for every node add its xmlData attribute it to an XML document in the same hierarchy as the tree.

I'm not sure if this is clear or not.

So let's say I have a scene graph like this:

<levelroot>
                        <playercharacter>Some information<playercharacter/>
</levelroot>

I want to write that xml to an xml document, preserving the hierarchy with levelroot being the root tag.
Does that make sense?
                         
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With levelroot being the root tag. Sorry, forgot to mention.

So the first xml element  would be <levelroot>. But when I try to add <levelroot> as the first tag in the xml doc, it throws the error above.
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I looked for information about JDOM root elements, I came across a book chapter which said this:

Elliotte Rusty Harold wrote:In this case the document begins its life in a temporarily malformed state, and any attempt to do almost anything with it except set a root element or add content to the document’s prolog will fail



The code in the example looks like this:



So I'd guess that calling the setRootElement would help. Alternatively there's this shortcut:


 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also the API documentation which says the same thing.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same error as before:



Error:
[codejava]
187 In Class::class GameMap.XMLAdapter Journal Entry:Serializing root element: LevelRoot
Exception in thread "Thread-8" org.jdom2.IllegalAddException: The element "LevelRoot" could not be added as the root of the document: The Content already has an existing parent document
at org.jdom2.ContentList.checkPreConditions(ContentList.java:208)
at org.jdom2.ContentList.add(ContentList.java:244)
at org.jdom2.ContentList.add(ContentList.java:79)
at java.base/java.util.AbstractList.add(Unknown Source)
at org.jdom2.Document.setRootElement(Document.java:238)
at GameMap.XMLAdapter.Serialize(XMLAdapter.java:239)
at GameMap.CHotLoader.Serialize(CHotLoader.java:206)
at GameMap.CHotLoader.loadNMap(CHotLoader.java:132)
at GameMap.CHotLoader.loadMap(CHotLoader.java:115)
at GameMap.CHotLoader.HotLoad(CHotLoader.java:84)
at MainGame.Loop.run(Loop.java:29)
[/code]
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:Same error as before:



Error:
[codejava]
187 In Class::class GameMap.XMLAdapter Journal Entry:Serializing root element: LevelRoot
Exception in thread "Thread-8" org.jdom2.IllegalAddException: The element "LevelRoot" could not be added as the root of the document: The Content already has an existing parent document
at org.jdom2.ContentList.checkPreConditions(ContentList.java:208)
at org.jdom2.ContentList.add(ContentList.java:244)
at org.jdom2.ContentList.add(ContentList.java:79)
at java.base/java.util.AbstractList.add(Unknown Source)
at org.jdom2.Document.setRootElement(Document.java:238)
at GameMap.XMLAdapter.Serialize(XMLAdapter.java:239)
at GameMap.CHotLoader.Serialize(CHotLoader.java:206)
at GameMap.CHotLoader.loadNMap(CHotLoader.java:132)
at GameMap.CHotLoader.loadMap(CHotLoader.java:115)
at GameMap.CHotLoader.HotLoad(CHotLoader.java:84)
at MainGame.Loop.run(Loop.java:29)
[/code]



Sorry to repeat myself; my post got lost inside the quote tag, perhaps no one saw it (or I am just off and people are being polite !) .

It really seems to me that this is telling you that the element "LevelRoot" is already a part of a Document ( a Node can't be the child of two documents). Is there any way that that is possible? "In that case the Content" in the message is LevelRoot or isome part of it. So yo're trying to add it here :



but root_element (where ever it is coming from) has not yet been severed from some other Tree.

OK I won't comment again LOL !!
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Simkulette wrote:It really seems to me that this is telling you that the element "LevelRoot" is already a part of a Document ( a Node can't be the child of two documents). Is there any way that that is possible?



Yes, now that I read the error message more carefully, that is just what it is telling you. And we know that the LevelRoot is part of the SceneGraphManager, which must surely be a JDOM Document otherwise its Elements would be incompatible with JDOM, so not only is it possible, it is a fact.

I guess the solution would be to create a new Element from the LevelRoot instead of using the LevelRoot itself as an Element.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, LevelRoot is not part of any other document. To make sure it isn't I changed the code to the following:




You'll notice that I'm creating a brand new Element, setting its text to "RootOne" and trying to add it to the new Document. It still returns the following LevelRoot error:



The XML file for which this references is:




 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you know what, it is adding RootOne. Its the next node down that it gives that LevelRoot error again. Here is the full output:

 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Sorry ahead of time this is going to be a long post. I use a local document object to load in the XML data before it gets altered in anyway. Then I use a serialize function later on, with a newlocal document object to write out to an xml file. Does it matter that I use two document objects if they are local to the methods? Here is the long part. This is my XMLAdapter class.



 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:Actually, you know what, it is adding RootOne. Its the next node down that it gives that LevelRoot error again.



Yeah, same thing. You have to add a copy of the element to the new tree.
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was confused from the start, when you described your act of copying an XML document to another XML document as "serializing". The meaning of the word "serialize" in the XML world is to transform an internal representation of XML (e.g. a SceneGraphManager or a JDOM Document) to an external representation (a text file). The opposite transformation is called "parsing".

So as a result I found your complete code very hard to read because I didn't understand what its purpose was. It unfortunately became TL;DR. So could you give us a short summary of the purpose of that code?
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yeah, same thing. You have to add a copy of the element to the new tree



What do you mean a copy of the element to the new tree? Can you expand?
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I was confused from the start, when you described your act of copying an XML document to another XML document as "serializing". The meaning of the word "serialize" in the XML world is to transform an internal representation of XML (e.g. a SceneGraphManager or a JDOM Document) to an external representation (a text file). The opposite transformation is called "parsing".

So as a result I found your complete code very hard to read because I didn't understand what its purpose was. It unfortunately became TL;DR. So could you give us a short summary of the purpose of that code?



That's the thing. The goal is to export the scenegraph to an xml file. So I am essentially serializing the SceneGraph to a text (xml) file.
What is TL;DR?

The purpose of the code is to first load each XML tag into a scenegraph, for example I may have a root tag that says "<LevelRoot>" and then a tag that says "<Loot>" and then a tag that says "<DaemonArmor>" nested within. I want to load those tags into SceneGraphNodes, with each node containing an XML Element like, for example, "<DaemonArmoir>" or "<Loot>".
The SceneGraph should be an exact mirror of the XML document loaded in.

Then, in the same class, but with different functionality, I want to serialize that scenegraph  back to an XML  file, basically reversing the process. So, since "<LevelRoot>" is the root node it should be that way in the XML file I'm outputting. Then, using the previous example, I want the next XML tag to be "<Loot>" and the following tag nested to be "<DaemonArmor>". So it should take the nodes in the SceneGraph and output them in order to an XML file.

Does that clarify things?
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:

I was confused from the start, when you described your act of copying an XML document to another XML document as "serializing". The meaning of the word "serialize" in the XML world is to transform an internal representation of XML (e.g. a SceneGraphManager or a JDOM Document) to an external representation (a text file). The opposite transformation is called "parsing".

So as a result I found your complete code very hard to read because I didn't understand what its purpose was. It unfortunately became TL;DR. So could you give us a short summary of the purpose of that code?



That's the thing. The goal is to export the scenegraph to an xml file. So I am essentially serializing the SceneGraph to a text (xml) file.
What is TL;DR?

The purpose of the code is to first load each XML tag into a scenegraph, for example I may have a root tag that says "<LevelRoot>" and then a tag that says "<Loot>" and then a tag that says "<DaemonArmor>" nested within. I want to load those tags into SceneGraphNodes, with each node containing an XML Element like, for example, "<DaemonArmoir>" or "<Loot>".
The SceneGraph should be an exact mirror of the XML document loaded in.

Then, in the same class, but with different functionality, I want to serialize that scenegraph  back to an XML  file, basically reversing the process. So, since "<LevelRoot>" is the root node it should be that way in the XML file I'm outputting. Then, using the previous example, I want the next XML tag to be "<Loot>" and the following tag nested to be "<DaemonArmor>". So it should take the nodes in the SceneGraph and output them in order to an XML file.

Does that clarify things?




You need to write the simplest thing that could possibly work in the smallest way with the tiniest throw-away pretend test data imaginable. Then you can see if what you're doing is just fundamentally flawed in a way you were not previously aware of. You have a forest of potential problems; you need just a tree.
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:That's the thing. The goal is to export the scenegraph to an xml file. So I am essentially serializing the SceneGraph to a text (xml) file.



But you aren't serializing the SceneGraph to a text file. Not in the original code, anyway. You're copying it to (another) JDOM document instead.

It appears a SceneGraph already is a JDOM document. So you should just serialize it directly. Here's a book chapter about how to serialize a JDOM document.

What is TL;DR?



Too Long; Didn't Read.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the SceneGraph is an object with nodes in it. Inside the nodes are xml data - elements. So I read in an xml document into the scenegraph, it is not an XML document itself. Then, later on, the scene graph is exported as an XML document. If TL;DR, then TL;DC - too long don't comment.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I figured out my problem. I have to detach the elements before I add them to the new document.

So my issue is this: How do I read in an xml document, detach the elements so they are no longer associated with the document,
and then put them in another document to be exported.

Right now it is reading the elements into the scene graph, but there are other elements being loaded as well that aren't in the scene graph
but still show up on the output document. Thanks everyone for the help so far.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main thing is that there are certain elements in the original XML file that I don't want in the output XML file
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've posted a couple of links now to an online book which contains information about JDOM. I suspect that many of your questions are answered by that book.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok  Thanks Paul I'll look into it
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul: I looked back at the posts and the only link I see is to the API. Could you post that link againhttps://coderanch.com/forums/topicBoost/boost/687492?
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the relevant chapter in the book: JDOM.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

I read the chapter in the JDOM book. While interesting, I do not think it provides a solution to my problem.

Question: When an XML document is parsed into JDOM and it becomes a tree structure, say I want to transfer that tree to another JDOM document. How do I do that piece by piece.
So,

I have a JDOM tree object
I take each element and put it in my own tree structure, each node corresponding to an element in the JDOM tree object
Then I want to iterate over my own tree structure to create a new JDOM tree object.
Then I want to serialize the new JDOM tree structure.

I think that's right, that's what I want to do.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could somebody post some pseudo code or kind of explain how this process would work? I seem to be getting nowhere.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I basically want to break down the JDOM2 tree that is loaded from the XML and build up a new Document for output
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:I basically want to break down the JDOM2 tree that is loaded from the XML and build up a new Document for output



Your problem has always been what I suggested above and what your error message was telling you . The Node is attached to some other tree. That's what that error message says.

You have to detach the node from the tree. Can you do the following?  Write a JDOM tree with one node. Detach that Node and attach it to another, empty JDOM tree? If you can't do that, if you can't get it to compile and  run that means  your whole approach is wrong and nothing based on it can possibly work. You may have to essentially use a copy constructor on the node's information to create a whole new node and attach that to a new JDOM tree.
 
Paul Clapham
Sheriff
Posts: 23439
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick look at the JDOM API documentation reveals that the Element class has a detach() method...
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It pays huge dividends to learn to write the tiniest possible program you think could work.

Most programming confusion comes from

1) false or insufficient understanding of the language

2) false or insufficient knowledge of the APIs

3) burying your logic in huge mounds of code that act as the haystack when it comes time to find the needle of "what's going wrong?". 

Of those 3, the first two are cured with enough time and experience. The third gets worse with time (you wrote MORE code !) and experience (you're now addicted to, even proud of, your bad coding style).

It is generally where  bugs and mental anguish come from and where time goes to.
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I saw the detach method and the removecontent methods...



So I'm sending the children of the node to a recursive function to process them. Then the current node is detached, content is removed, and it is added to my scenegraph.
But it appears that the node is not being detached or the content is not being removed.

I'll try a small program as well.

-Ted
 
Ted Gress
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

In this case I don't think I can write a tiny program. There are data structures that are linked to this and writing a tiny program to test the code would essentially force to me to rewrite the whole system just to test it.

For example, I would have to implement the scenegraph class. T he program is as small as it can be. Thanks for the advice though.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ted Gress wrote:David,

In this case I don't think I can write a tiny program.



Compilable and runnable:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!