• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java and Xml

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Right now I am doing:

when the tmpElement is some node with value. So when I am standing on the node and doing getFirstChild() I am on the value (node) of this node. Then I do getValue() and I have the value as a string which is what I need.
so if tmpElement is on node then value will be "value".

My problem is that now the value can be xml tree.
for instance:

So now tmpElement.getFirstChild() gives me node1. What I need is to get all of the internal tree:

as a string.

Thanks,
John.
 
Paul Clapham
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to convert from internal representation (e.g. DOM nodes) to external representation (file, string, etc) that's called "serializing". (The opposite is called "parsing", as you probably know already.)

To serialize data from a DOM, the easiest way is to use a Transformer and call its transform(source, result) method. In your case the input is coming from a DOM node so the source would be a DOMSource based on that node. Output to a String is trickier unless you dig through the API documentation a bit more than superficially. Create your Result like this:
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes but how do I get the whole internal tree? Forget toString() for a sec.
 
Paul Clapham
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said "as a string" in your original post. Is that not what you want? Because you already have that tree in your DOM.
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did say string.
So getFirstChild() gives me the whole tree? not just the first node?
 
Paul Clapham
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Landon:
So getFirstChild() gives me the whole tree? not just the first node?
In your example it gives you a node, yes. But that node has children, which in turn can have children.

As I mentioned in my first reply, parsing converts the XML document to an internal format. In your case it's a DOM, which is a tree structure containing the same data that the original document contained. You can write code to navigate to any node in that tree at all.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Watch out for getFirstChild() - if your XML document contains any text formatting for readability, the result is likely to be a Text Node, not the Element you were expecting. This commonly causes confusion for beginners.

Use methods that name the Element to avoid this.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic