Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to parse xml which is in the form of string

 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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 ]
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic