Search code examples
javaxmldomjakarta-eesax

Parsing XML Elements in JAVA


  1. I want to parse XML in java. It will be DOM or SAX. Read in a book JAXP is good one. Also when i google out found XERCES/XALAN. Which parser is commonly used?

  2. In SAX Parser if i register for a single element event will the SAX parse stops processing the XML message after encountering the element. Read in a book DOM reads the entire XML and loads into memory even if i want to know single element value.


Solution

    1. All of the above parsers you mention are excellent. My personal preference would be XERCES if the application did a lot of XML processing, otherwise, the "built in" parsers are more than good enough.

    2. You will need to process every event from the SAX parser and ignore the ones you are not interested in. You can stop parsing at any point by "destroying" the parser object. If you are only interested in one or two elements of a large message then SAX is the way to go. If you are interested in all or most of the elements then use the DOM parser, you take a slight performance hit, but, the "give me what I want" API makes for much clearer code than the "take what I give you" SAX API.