Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can SAX Parsers use XPath in Java?

Tags:

java

dom

xml

xpath

sax

I'm trying to migrate one of my classes which uses DOM parsing with lots of XPath expressions to SAX parsing. DOM Parsing was good for me but some of the files i try to parse are too big and they cause server timeouts. I want to reuse the XPath with the SAX parsing but i'm not sure if it is possible and if not possible could you please help me because i have no idea how the following code will be when i use only SAX:

Document doc = bpsXml.getDocument();
String supplierName = BPSXMLUtils.getXpathString(doc, "/Invoice/InvoiceHeader/Party[@stdValue='SU']/Name/Name1");
String language = BPSXMLUtils.getXpathString(doc, "/Invoice/InvoiceHeader/InvoiceLanguage/@stdValue");
like image 466
Nikola Dichev Avatar asked Jan 09 '12 16:01

Nikola Dichev


2 Answers

IMHO the easiest way to process XML is to use StAX, the Streaming API for XML. It combines the advantages of DOM and SAX (and offers an easier migration to you). You still have a cursor to an XML element (like in SAX), but your code moves the cursor forward. This gives the great advantage that XML processing code becomes much more readable. It also solves the memory issue as only the current XML element has to be held in the memory. Here's also a nice tutorial.

To also answer your original question: A short search on Google showed to me there is no easy, widely accepted way which probably means that all custom solutions are not robust, not maintained and not well-tested.

like image 26
stdll Avatar answered Oct 08 '22 11:10

stdll


Simply using a SAX parser will not build a representation of your XML tree in memory (this is why SAX is more memory-efficient). It will only trigger "events" whenever a new XML element is encountered. You will have to keep the context (often a stack of parent elements) in memory to "know" where you are in the tree.

Since you will not have a tree in memory, you will not be able to use XPath. You can only test for the current "context" (your manuallay managed stack) to query your document.Remember that the SAX parser will only do one run on your file, so order in the file is important.

Fortunately, there are other approach like VTD-XML which is a library that build the XML tree in memory, but only the structure part, it does not extract the actual content from the file, the content is extracted as-needed. It is much more memory efficient than a DOM parser while still allowing XPath. I personnaly use this library at work to parse ~700MB XML files with XPath (yes that's insane but it works and it is very fast.)

like image 87
Vincent Robert Avatar answered Oct 08 '22 12:10

Vincent Robert