aspose file tools*
The moose likes Java in General and the fly likes Java and Xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java and Xml" Watch "Java and Xml" New topic
Author

Java and Xml

John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
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
Bartender

Joined: Oct 14, 2005
Posts: 18163
    
    8

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

Joined: Sep 25, 2008
Posts: 227
Yes but how do I get the whole internal tree? Forget toString() for a sec.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18163
    
    8

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

Joined: Sep 25, 2008
Posts: 227
Yes I did say string.
So getFirstChild() gives me the whole tree? not just the first node?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18163
    
    8

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

Joined: Mar 22, 2000
Posts: 12682
    
    5
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


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java and Xml
 
Similar Threads
Clustering on JBoss3.2+ & 4+
Help parse using JAXP
Replicate XML Node using XSL
how to retrieve attribute value of a node
Tomcat session replication on failover in a cluster