Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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: 43026
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic