File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes A simple way to Iterate on XML tree and extract data from it ? DOM or SAX or Xpath ...? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "A simple way to Iterate on XML tree and extract data from it ? DOM or SAX or Xpath ...?" Watch "A simple way to Iterate on XML tree and extract data from it ? DOM or SAX or Xpath ...?" New topic
Author

A simple way to Iterate on XML tree and extract data from it ? DOM or SAX or Xpath ...?

david ron
Greenhorn

Joined: May 05, 2012
Posts: 7
Assuming that I have four levels in my XML tree , where level 3 can have the same son - twice , i.e.
in the following XML :


I have two `Doors` for each `Round` , then the question is , using `Dom` or `Sax` (or Jdom if it helps) can

I iterate on my tree and get the data in every level ?

At the moment I "went" a level down and got the Rounds , here :




but it seems a lot of code for iteration .

Is there a simple way to extract the data of that XML ? assume that I have 2 Doors per round and someK number of Rounds .

Thanks
David
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Hi,

If I need xml processing like yours, I will using Spring and its OXM support. You will need to create a schema for your xml data. After that, it will very easy and less error prone.
david ron
Greenhorn

Joined: May 05, 2012
Posts: 7
Jesus Angeles wrote:Hi,

If I need xml processing like yours, I will using Spring and its OXM support. You will need to create a schema for your xml data. After that, it will very easy and less error prone.



Hello my friend , sorry but I'm allowed to use that (HW) .
I can use SAX / DOC / JDOM / Xpath .

Do you know maybe a simple way to parse that XML using one of the above ?
I've been trying to do it for the last two days but nothing works .

thanks
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Try the link to an online doc posted at http://www.coderanch.com/t/541702/XML/element-name-parsing#2457639.
david ron
Greenhorn

Joined: May 05, 2012
Posts: 7
Jesus Angeles wrote:Try the link to an online doc posted at http://www.coderanch.com/t/541702/XML/element-name-parsing#2457639.

I've already tried this , the link of SAX inside the book is broken .

Can you please direct me to a link to explain how exactly can I get the entire XML file above into my own data structure ?
I've tried with DOM & SAX but nothing worked since I have a little complex XML file , as you can see.

Regards
David
T Dahl
Ranch Hand

Joined: Oct 07, 2010
Posts: 35
For simple retrieval as in your case it is hard to beat XPath. If you are going to work a lot with XML take the effort to learn XPath. You have to get used to indexes starting from 1 - not 0

I don't do much XML in Java, but when I do I first think XOM for structured data (as in your case) and DOM for mixed content. DOM is almost but not entirely compatible with the XDM model used by XPath.
david ron
Greenhorn

Joined: May 05, 2012
Posts: 7
T Dahl wrote:For simple retrieval as in your case it is hard to beat XPath. If you are going to work a lot with XML take the effort to learn XPath. You have to get used to indexes starting from 1 - not 0

I don't do much XML in Java, but when I do I first think XOM for structured data (as in your case) and DOM for mixed content. DOM is almost but not entirely compatible with the XDM model used by XPath.


Frankly I know about Xpath for exactly 3 hours . From my understanding coding in Xpath is much more preferable than others since the it doesn't require too much coding , is it correct ?

So you're saying to use Xpath ? I've been trying to use it in order to extract the data for the XML above however came up empty handed . Any directions ?

Thanks !
T Dahl
Ranch Hand

Joined: Oct 07, 2010
Posts: 35
3 hours isn't much for learning XPath

In general, worry more about clarity and maintainability than quantity of code.
XPath is tailor made for XML. It's powerful and easy to read ones you know it. It integrates very well with XSLT and reasonably well with other languages such as Java.

I'll give you some ideas rather than solutions. You navigate the document tree in a similar way to how you would navigate a file tree. E.g. /Game/Round/Door. Counting your rounds could be done like this:
Listing the names of all the first doors:

The [1] makes sure it is only the first door. If you wanted all doors of the second Round:


As a Java programmer you may be used to thinking in Procedures. In XPath everything is about thinking functions. As a Java programmer you might be tempted to iterate over all the Doors, printing "First door:" then doing an xpath constructed in Java with an index you maintain in Java. That's just clumsy. Instead you could use the XPath function concat in the return expression. Much easier to read (and in this case less code). I leave it to you to figure out the details.

david ron
Greenhorn

Joined: May 05, 2012
Posts: 7
T Dahl wrote:3 hours isn't much for learning XPath

In general, worry more about clarity and maintainability than quantity of code.
XPath is tailor made for XML. It's powerful and easy to read ones you know it. It integrates very well with XSLT and reasonably well with other languages such as Java.

I'll give you some ideas rather than solutions. You navigate the document tree in a similar way to how you would navigate a file tree. E.g. /Game/Round/Door. Counting your rounds could be done like this:
Listing the names of all the first doors:

The [1] makes sure it is only the first door. If you wanted all doors of the second Round:


As a Java programmer you may be used to thinking in Procedures. In XPath everything is about thinking functions. As a Java programmer you might be tempted to iterate over all the Doors, printing "First door:" then doing an xpath constructed in Java with an index you maintain in Java. That's just clumsy. Instead you could use the XPath function concat in the return expression. Much easier to read (and in this case less code). I leave it to you to figure out the details.



This is indeed very nice , I've tried it for a little while and the code is pretty good :



I'll try to work with the above examples , thanks a lot !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A simple way to Iterate on XML tree and extract data from it ? DOM or SAX or Xpath ...?