XML Streaming Parser from Series of byte[] or ByteBuffer(s)
843834Jan 30 2008 — edited Jan 31 2008I am using NIO to asynchronous communicate with non-blocking sockets.
I need to parse the messages over these sockets as XML. I figured SAX would work like EXPAT (which I am familiar with), but if I try to feed a SAX parser bytes as I receive them (using Xerces 2.9), I get:
SAXParseException: XML document structures must start and end within the same entity.
When I go back to EXPAT 1 using Xerces 1.4:
SAXParseException: The root element is required in a well-formed document.
My NIO class structure will feed me peices of the XML protocol. I just need callbacks when a tag begins (with attributes) and a tag ends. It seems like these parsers are completely stateless and do not buffer partial tags.
I looked at STAX (java.xml.stream.*); but it too opens a file and it is unclear if it can simply be fed bytes as I get them as I need (STAX sample included from http://sjsxp.dev.java.net):
-----
XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename, new FileInputStream(filename));
int eventType = xmlr.getEventType();
printStartDocument(xmlr);
while(xmlr.hasNext()){
eventType = xmlr.next();
printStartElement(xmlr);
printEndElement(xmlr);
printText(xmlr);
printPIData(xmlr);
printComment(xmlr);
}
-----
So, my question is: How can I accomplish an EXPAT style streaming XML parser without simply wrapping EXPAT using the JNI?
In other words, is there away to feed any of these APIs raw bytes from an asynchronous stream? If not, is there an API that can do this?
James
Beverly, MA