Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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

reading xml file in Android

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using yahoo local search web service in my android application. I am getting xml reply from yahoo local search query. I want to know do i need to parse that xml file to read? and how can i read specific elements of all results. there are multiple elements of a single result. I want to read few elements of all results. any suggestion or code exmple would be really helpful. Thanks in advance.
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DocumentBuilderFactory and SAXParserFactory are both part of the Java API supported by Android. Start with one of these. DocumentBuilder / DocumentBuilderFactory requires more memory but is a bit easier to work with. SAXParser / SAXParserFactory doesn't store the entire XML document in memory but you need to keep track of the current state yourself.
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're not fussed about the size of your APK, you could consider using XStream. Whilst it might not be the best option, its certainly is an option. I wrote a noobs guide here
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, if you don't want dedicated Java objects for your XML elements, you can use the Jaxen library and access specific XML elements via XPath.
 
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm currently working on an app that reads several xml files from a server. At first glance the DocumentBuilder approach looked easiest. But if you take some time to dive into the matter, you'll probably find the SAX approach more clear and in the end easier to use. At least I did. Also, at a certain point I needed to fetch data between open- and close tags and the DocumentBuilder approach can't do that .
 
Rob Spoor
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really? So you can't get any instances of org.w3c.dom.Text from your Document? With that you can use getNodeValue() or getData() to get the actual text.
 
Misha van Tol
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I stand corrected, which proves never to rely solely on what you read on the web.
Nevertheless, I still find the SAX approach - if you get past the initial learning curve - the easier way.

BTW, besides the org.w3c.dom.Text approach it is actually possible to grab the data directly:

 
reply
    Bookmark Topic Watch Topic
  • New Topic