File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading XML data

 
Sanju Shah
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic