Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Parsing XML, should I use SAX or DOM?

 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to parse some RSS feeds for my website, and I'm not really sure where to start. Can anyone recommend any tutorials or articles? Should I use SAX or DOM to do it?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend you legendary tutorial/faq web by Roseanne Zhang.
My opinion is to use JDOM, parsing this way...
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, after some reading, here I am...
For some reason the node_x.getNodeValue call doesn't return the text, can anybody help me figure out why?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I am remember correctly, the 'ELEMENT' node type has broader range. So can you check the nodetype static variable again and try 'TEXTNODE' or something like that? Hope this helps you.
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That didn't work for me either. :dunno:
[ July 21, 2003: Message edited by: Chris Stewart ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the inner most loop of your code, you got to the point where you can print the node name. For example, "title"
<title>Open source or no: Let the market decide</title>
The node you are examining at this point has a node name "title" and node value "null". I assume you are trying to print the text "Open source....".
It is represented by the child node and this child node has a node name "#text" (or null for some parsers) and the node value "Open source..."
To get your program working, get the child node, check it's Node type (Node.TEXT_NODE) and then get the NodeValue.
 
Chris Stewart
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect, thank you! This is my final code:
 
Naren
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can remove a lot of lines from your code using the call getElementsByTagName("tag_name").
I use this function to get the value of a node:

the 'for' loop inside is just to be safe with all sorts of parser implementations. The text value of the node is represented, some times, in multiple child nodes.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the XML forum.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic