views:

538

answers:

5

I'm looking for an XML parser that instead of parsing from an InputStream or InputSource will instead allow blocks of text to be pushed into the parser. E.g. I would like to have something like the following:

public class DataReceiver {
    private SAXParser parser = //...
    private DefaultHandler handler = //...

    /**
     * Called each time some data is received.
     */
    public void onDataReceived(byte[] data) {
        parser.push(data, handler);
    }
}

The reason is that I would like something that will play nice with the NIO networking libraries rather than having to revert back to a thread per connection model required to support a blocking InputStream.

+4  A: 

Edit: Now I see. You receive the XML in chunks and you want to feed it into a proper XML parser. So you need an object, which is a queue at the one end, and an InputStream at the other end?

You could aggregate the byte arrays received into a ByteArrayOutputStream, convert it to ByteArrayInputStream and feed it to the SAXParser.

Or you could check out the PipedInputStream/PipedOutputStream pair. In this case, you'll need to do the parsing in another thread as SAX parser uses the current thread to emit events, blocking your receive().

Edit: Based on the comments I suggest taking the aggregation route. You collect the chunks into a ByteArrayOutputStream. To know whether you received all chunks for your XML, check if the current chunk or the contents of the ByteArrayOutputStream contains your end tag of the XML root node. Then you could just pass the data into a SAXParser which can now run in the current thread without problems. To avoid unnecessary array re-creation you could implement your own unsynchronized simple byte array wrapper or look for such implementation.

kd304
The PipedInputStream/PipedOutputStream looks like a good way to go to provide traditional parsers with the InputStream and push in chunks.
Robin
This approach still requires a thread for each client connection, it just shifts the problem to a different area of the code. If I am to go with thread per connection model, then I would probably just use traditional Sockets and InputStreams as the solution would be simpler.
Michael Barker
Beats me. I never heard a Java parser having such a feature you are looking for. I guess its very hard to implement one without an equivalent of the C# yield return construct - e.g. the management of stopping the parsing and return to the caller and later on continue is a complex state-machine thing.
kd304
+2  A: 

This is a recent (April 2009) post from the Xerces J-Users mailing list, where the original poster is having the exact same issue. One potentially very good response by "Jeff" is given, but there is no follow up to the original poster's response:

http://www.nabble.com/parsing-an-xml-document-chunk-by-chunk-td22945319.html

It's potentially new enough to bump on the list, or at very least help with the search.

Edit

Found another useful link, mentioning a library called Woodstox and describing the state of Stream vs. NIO based parsers and some possible approaches to emulating a stream:

http://markmail.org/message/ogqqcj7dt3lwkbov

Harlan Iverson
Good findings. Gave me an idea. As Xerces is open source, get it (or in fact any open source XML parser that is small enough) and hack the position where it reads the bytes from the input stream and create a clever way state-save state-restore to allow an early return / continue. But I have no clue how to implement it.
kd304
possibly good call on that hacking. also, I emailed the original poster and sent him this thread. I hope he's figured something out and will share. *crosses fingers*
Harlan Iverson
A: 

Hi, I'm sorry, I didn't managed to solve this problem. I could not find a parser like the one I need. But I'm thinking to write one by my self. A very simple one: just as fisibility study, but enough to solve my problem and hopfully yours. Unortunatelly I have been very buisy and the next two weeks I'll be out, but maybe in july I'll start working on it. I'll let you know as soon as I have something working.

mt

If you do let me know. If you can make it open source I will hopefully be able to contribute.
Michael Barker
I don't know, but maybe the Yielder framework could help in it? See http://chaoticjava.com/posts/category/code/java/frameworks/yielder/ for the details. The inner workings is ugly though as it used bytecode re-enginering to support the early-return-continue-later format.
kd304
+1  A: 

Check openfire's XMLLeightweightParser and how it generates XML messages from single chunks because of NIO. The whole project is a great source for answers regarding NIO and XMPP questions.

This is the closest I've seen, unfortunately they don't offer it as a separate library and the don't translate into XML object (Elements, Attributes etc.)
Michael Barker
XMLLeightweightParser can easily be used standalone. It just assures that you have complete xml tags. Openfire then feeds these chunks into XPP3 to parse a complete Element object. I am using the same thing in XMPP inspired server and it works like a charm.
That still has to require reading chunks longer that are strictly necessary, given that xpp3 uses blocking io (like most other pull parsers). That can be better than fully blocking, esp. if you explicitly frame pieces, but it's not quite ideal.
StaxMan
+1  A: 

Surprisingly no one mentioned one Java XML parser that does implement non-blocking ("async") parsing: Aalto. Part of the reason may be lack of documentation (and its low level of activity). Aalto implements basic Stax API, but also minor extensions to allow pushing input (this part has not been finalized; functionality exists but API is not finalized). For more information you could check out related discussion group.

StaxMan
I didn't come up in a number of Google searches, I'll check it out.
Michael Barker
Yeah, it's not easy to find with generic searchs, given its low profile. Hopefully that will change soon since there are some more developers interested in it.
StaxMan
Aalto was recently relicensed as Apache License too: http://github.com/FasterXML/aalto-xml
Alex Cruise