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 Parsing XML, should I use SAX or DOM? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing XML, should I use SAX or DOM?" Watch "Parsing XML, should I use SAX or DOM?" New topic
Author

Parsing XML, should I use SAX or DOM?

Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
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?
Pavel Halas
Ranch Hand

Joined: Jul 05, 2003
Posts: 35
I recommend you legendary tutorial/faq web by Roseanne Zhang.
My opinion is to use JDOM, parsing this way...
Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
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?
fei long
Ranch Hand

Joined: Apr 04, 2002
Posts: 48
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

Joined: Sep 14, 2002
Posts: 184
That didn't work for me either. :dunno:
[ July 21, 2003: Message edited by: Chris Stewart ]
Naren
Greenhorn

Joined: Jul 21, 2003
Posts: 23
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.


<a href="http://www.mqtips.com" target="_blank" rel="nofollow">MQMessenger</a> - free Java app for MQSeries
Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
Perfect, thank you! This is my final code:
Naren
Greenhorn

Joined: Jul 21, 2003
Posts: 23
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.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Moving to the XML forum.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing XML, should I use SAX or DOM?
 
Similar Threads
how to create DOM structure from SAX parser
Parsing XML, should I use SAX or DOM?
App chokes when SAX-parsing an XML file without a DTD file
Difference Between DOM and SAX
DOM and SAX