File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes How to parse xml which is in the form of string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to parse xml which is in the form of string" Watch "How to parse xml which is in the form of string" New topic
Author

How to parse xml which is in the form of string

Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
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

Joined: May 14, 2004
Posts: 512
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


Regards,
Amit
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: May 14, 2004
Posts: 512
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to parse xml which is in the form of string