Please advise which is better to use xml
handler in Java
(preferably with reference to the source).
2 answers
What source can we talk about? Java has two ways to handle XML on board: SAX and DOM.
SAX is an XML parser streaming by looking at the visitor pattern: you give it the source stream and your handler implementation, which it will call as it finds tags, content parts, etc.
DOM - in this case, you can use the DOM builder to build a DOM tree from your XML document, and then use the DOM API to work with this tree.
The difference between them is clear: working with DOM in some cases is much easier than with SAX, but the DOM tree of the entire document can take a lot of memory .. it may turn out that it simply does not fit in memory. While SAX allows you to process a document not all at once, so you can process a document of any size. On the other hand, at times SAX document processing can also be very convenient, so you should not think that you should always choose DOM if the document is small.
- Thank you enlighten me. - Anton Mukhin
- And it’s possible a third-party library, I really liked dom4j - Dex
- Do you think it is more convenient than others? - Anton Mukhin
- dom4j has a nice API, but draws a lot of dependencies. Besides, if I understood correctly, then it is no longer supported. - cy6erGn0m
- I think it would be best to use standard handlers (DOM or SAX). And there is a lot of literature in Russian, and there is no need to connect additional libraries. - Anton Mukhin
In addition to DOM and SAX, I recommend to consider the possibility of using StAX. It is also a streaming event API, but it works somewhat differently (for example, you can request the next element yourself, or you can write to another XML document at the same time).
In some cases, StAX runs faster than DOM and SAX implementations and requires less memory.
StAX provides two APIs from which to choose according to the task.
Introduction to StAX in Russian - Using StAX for XML Processing: Introduction to Streaming API for XML (StAX)
Comparison of different approaches for parsing XML in English - Why StAX?
- Thanks for the advice - Anton Mukhin