• 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

How to parse xml which is in the form of string

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have XML which is in the form of string.I need to parse it. I am using SAXParser. I am not able to do that.

The xml string is


Besides i want to store the values in a java bean how do i do that


Thanks in advance

[ November 05, 2007: Message edited by: Neha Mohit ]

(Edited so the XML is in more than one line - PC)
[ November 06, 2007: Message edited by: Paul Clapham ]
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
You can use void parse(InputStream is, DefaultHandler dh) of SAXParser.
The InputStream object you use in this case can be ByteArrayInputStream which is instantiated using byte[].(String.getBytes()).
You need to extend DefaultHandler class and then populate the Java bean while reading the XML using callback methods.

This may not be the only way doing this. I just figured it out by looking at Java docs.

regards,
amit
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better still: the parse() method takes an InputSource. Notice that there is a constructor of InputSource that takes a Reader. Notice also that there is a subclass of Reader named StringReader. Those two things are what you need.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Originally posted by Paul Clapham:
Better still: the parse() method takes an InputSource. Notice that there is a constructor of InputSource that takes a Reader. Notice also that there is a subclass of Reader named StringReader. Those two things are what you need.



Thanks for the information. This will be certainly a better way of parsing a string XML.

Thanks,
Amit
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic