Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

method to parse streaming XML?

Status
Not open for further replies.

riches85

Programmer
Nov 13, 2002
59
0
0
US
Has anyone had the experience of parsing streaming XML? I need to be able to parse XML over a socket connection in Java and most parsers expect a whold document at once. Im curious if anyone has found a way to do it and can steer me in the right direction. Thank you in advance.
 
You can just read it like any other input stream, eg :

Code:
Socket s = // however you retrieved your Socket
InputStream in = s.getInputStream();
boolean strict = true;

try {
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	DocumentBuilder builder = null;
	if (strict) {
		factory.setNamespaceAware(true);
		factory.setValidating(true);
		builder = factory.newDocumentBuilder();
		builder.setErrorHandler(new XMLErrorHandler());
	} else {
		builder = factory.newDocumentBuilder();
	}

	Document doc = builder.parse(in);
	return doc;
	} catch (SAXException se) {
		se.printStackTrace(System.err);
		//throw new XMLException(se.toString());
	} catch (ParserConfigurationException pce) {
		pce.printStackTrace(System.err);
		//throw new XMLException(pce.toString());
	}
 
will this parse xml as it comes in or will it wait for the end of file marker before it starts partsing?
 
it will read the current data in an input stream until the tcp socket is closed.
 
also please do not crosspost with other forums here (XML forum)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top