File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading XML data" Watch "Reading XML data" New topic

Reading XML data

Sanju Shah

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.... >


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..

Roseanne Zhang
Ranch Hand

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

Q. How to parse an XML String?
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
I agree. Here's the link:
subject: Reading XML data
It's not a secret anymore!