aspose file tools*
The moose likes Java in General and the fly likes Reading XML data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading XML data" Watch "Reading XML data" New topic
Author

Reading XML data

Sanju Shah
Greenhorn

Joined: Apr 06, 2006
Posts: 19
Hello ,

I'm farily new to java & XML .So please excuse me if its a silly question. I'm required to read Data from a socket which is in XML format.Below is my code.

String input = null;
StringBuffer temp = new StringBuffer();
Socket soc = new Socket(ipaddress,port);
BufferedReader bread = new BufferedReader( new InputStreamReader( soc.getInputStream() ) );
while ((input = br.readLine()) != null)
{ temp.append(input);
}
System.out.println("Input is " + temp.toString());

The data that I get from the socket is of the format..
<XMl cersion.... >
<Node1>
<subnode1>abc</subnode1>
<subnode2>abc</subnode2>
<subnode3>abc</subnode3>

</Node1>

My question is I need some direction as to how I can convert the data that I have got to a XML document and use SAX praser to parse the data.. Please help..

Thanks
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
What you got is a String!

Q. How to parse an XML String?
A:
Easy! No matter you using DOM (javax.xml.parsers.DocumentBuilder) or SAX (org.xml.sax.XMLReader), or vendor specific SAXParser/DOMParser, you all can use the same signature method parse(InputSource input) to parse you XML. Now the question becomes how to use String to make an InputSource.

1. Contruct a StringReader by using your XML String.
2. Contruct an InputSource by using your StringReader
3. Parse the InputSource by using parse(InputSource input)

Copied from http://bobcat.webappcabaret.net/javachina/faq/xml_java_01.htm#jaxp_sax_Q05
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading XML data