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

How to read XML data

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have XML data in one string, and I want to extract data from its tag, how to do that in J2SE.

eg. My Data:

String str = "<tag1>data1</tag1><tag2>data2</tag2>"

how to read data1 and data2 from string.. ??

Note: I am using Google Health API to extract the profile and it is returning profile information in the form of XML.
Please suggest some way to read it....
Thanks and Regards
Amit
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, note that what you posted is not valid XML - it does not have one single root element. I hope that's not actually what Google Health uses.

Both the DOM and SAX APIs can read from an InputStream (which is easily constructed from a String). Check the java.xml.parsers.DocumentBuilder and SAXParser javadocs for the relevant methods. Basic examples of how to use both can be found here, particularly e510 and e517.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf, you mean not well formed?

You could also try StAX. StAX is a core JSE API in 1.6 and comes with a bundled implementation.

this and this should help if you are interested.
reply
    Bookmark Topic Watch Topic
  • New Topic