Search code examples
xmlparsingliferaysaxliferay-7

How to parse a huge XML file in Liferay with Java?


I have always been told that when parsing huge XML files, the memory-heavy DOM parsers should be avoid in favour of the lightweight SAX parsers.

I want to parse a huge XML file in Liferay. The file is just a list of items, and I need to process the items one-by-one independently, so it is a perfect use case for SAX.

I have found Liferay's SAXReaderUtil, but despite the "SAX" in its name, it looks suspiciously like a DOM parser. It has no events, and it seems to be all about returning a Document and selecting nodes with XPath. Am I mistaken?

What is the correct way to parse a huge XML file when developing a Liferay module?


Solution

  • There's nothing forcing you to utilize Liferay's XML Utils. Based on the way you're asking the question, I'm assuming that you're well experienced in parsing xml and its caveats. As a corollary I'm assuming that "you're doing it in Liferay like anywhere else" might be enough.

    Ideally, the XML parser/processor that you're deciding for should come as an OSGi bundle (or you find one already deployed in Liferay).