• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read XML data

 
Amit Bathla
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic