Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
XML and Related Technologies
Reading/Parsing an XML doc Question
Russ Russell
Ranch Hand
Posts: 72
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I have the following class which successfully reads and parses an XML
doc
. The problem is, it only reads the first <item> in the list.
What do I need to do in order to loop through the doc so that I can read the first few (or all) of the items? Here is my code:
package com.russ.RSS; import java.net.MalformedURLException; import java.net.URL; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.jaxen.JaxenException; import org.jaxen.XPath; import org.jaxen.dom4j.Dom4jXPath; public class RSS { String itemTitle; String itemPubdate; String itemLink; String itemDescription; String returnString[]; //CONSTRUCTOR public RSS(){ } public String[] getHeadline() { returnString = new String[3]; SAXReader reader = new SAXReader(); Document document; try { document = reader.read(new URL( "http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=school+district+budget+short&adult_ok=1")); XPath expression = new Dom4jXPath("/rss/channel"); Node result = (Node) expression.selectSingleNode(document); itemTitle = result.valueOf("//item//title"); itemPubdate = result.valueOf("//item//pubDate"); itemLink = result.valueOf("//item//link"); itemDescription = result.valueOf("//item//description"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (JaxenException e) { e.printStackTrace(); } returnString[0]=itemTitle; returnString[1]=itemDescription; returnString[2]=itemLink; return returnString; } }
Rob Spoor
Sheriff
Posts: 22781
131
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How about calling selectNodes instead of selectSingleNode? Because the latter selects only a single node -- hence the name.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Russ Russell
Ranch Hand
Posts: 72
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks! And then how do I loop through the items now that I amselecting multiple nodes?
Thanks again.
Rob Spoor
Sheriff
Posts: 22781
131
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
selectNodes returns a java.util.List; I'm sure I don't need to tell you how to iterate over a List. Because the List isn't generic the only thing you'll need to do is cast the elements to Node; but you need to do that for selectSingleNode too.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Russ Russell
Ranch Hand
Posts: 72
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks for the help so far.
I am VERY new to XML (and sort of new to
Java
).
It seems I am still only getting the first item. Does this look right?
List results = expression.selectNodes(document); Iterator iterator = results.iterator(); Navigator navigator = expression.getNavigator(); while (iterator.hasNext()) { Node result = (Node) iterator.next(); String value = StringFunction.evaluate(result, navigator); //This prints the entire document: System.out.println(value); //This prints only the fist element of <item><description> System.out.println(result.valueOf("//item//description")); }
I would like to print all the <item><description> elements in the entire document.
Thank you,
-Russ
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
xml updations and deletions
Need Help Iterating Elements Using dom4j
Null pointer exception probably due to synchronisation issues; problem in starting activity
Problem with dom4j "Getting started" example
Help regarding how to check the element entered is equal or not in xml file
More...