views:

119

answers:

2
+3  Q: 

libxml2 from java

Hi, This question is somewhat related to http://stackoverflow.com/questions/530064/fastest-xml-parser-for-small-simple-documents-in-java but with a few more specifics.

I'm working on an application which needs to parse many (10s of millions), small (approx. 300k) xml documents. The current implementation is using xerces-j and it takes about 2.5 ms per xml document on a 1.5 GHz machine. I'd like to improve this performance. I came across this article

http://www.xml.com/pub/a/2007/05/16/xml-parser-benchmarks-part-2.html

claiming that libxml2 can parse about an order of magnitude faster than any java parsers. I'm not sure if I believe it, but it caught my attention. Has anyone tried using libxml2 from the jvm? If so, is it faster than java dom parsing (xerces)? I'm thinking I'd still need my java dom structure, but I'm guessing that copying from a c-structured dom into java-dom shouldn't take long. I must have java-dom - sax will not help me in this case.

update: I just wrote a test for libxml2 and it wasn't any faster than xerces... granted my c coding ability is extremely rusty.

update I broadened the question a bit here: http://stackoverflow.com/questions/3825206/why-is-sax-parsing-faster-than-dom-parsing-and-how-does-stax-work and am open to the possibility of ditching dom.

Thanks

A: 

First of all, your question does not contain a question. What do you want to know?

I suppose you were using JNI to convert the c-dom into a java-dom. I dont know if there are official numbers, but in my experience c+JNI often is slower than directly doing it in java.

If you really want to speed up your processing, try to get rid of the dom (why do you need it? Maybe we can think of a solution together). If all xml files have the same schema, use your own specialized data model (and a SAX parser).

If you only use a subset of xml (i.e. without namespaces, only few attributes), consider writing your own parser that directly produces more efficient java objects (but I would not recommend that).

Arian
I bolded and added question marks.In regards to ditching DOM - I can't and I'm not interested in explaining why.
andersonbd1
Thank you for your input. I changed my mind. I'm open to ditching dom if I can and explaining more about what I'm doing. I created a new question here:http://stackoverflow.com/questions/3825206/why-is-sax-parsing-faster-than-dom-parsing-and-how-does-stax-work
andersonbd1
+1  A: 

In Java, StAX JSR-173 is generally considered to be the fastest approach to parsing XML. There are multiple implementations of StAX, the Woodstox implementation is generally regarded as being fast.

To improve performance I would avoid DOM. What are you doing with the XML? If you are ultimately dealing with it as objects, the you should consider an OXM solution. The standard is JAXB JSR-222. JAXB implementations such as MOXy (I'm the tech lead) will even allow you to do a partial mapping which will improve performance:

Blaise Doughan
In regards to ditching DOM - I can't and I'm not interested in explaining why.
andersonbd1
Thank you for your input. I changed my mind. I'm open to ditching dom if I can and explaining more about what I'm doing. I created a new question here:http://stackoverflow.com/questions/3825206/why-is-sax-parsing-faster-than-dom-parsing-and-how-does-stax-work
andersonbd1