• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the link to an online doc posted at https://coderanch.com/t/541702/XML/element-name-parsing#2457639.
 
david ron
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesus Angeles wrote:Try the link to an online doc posted at https://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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !
 
reply
    Bookmark Topic Watch Topic
  • New Topic