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?
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.
5 Answers
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?
Xerces is an implementation of both DOM and SAX, and it is built into the JDK. See javax.xml.parsers.
In SAX Parser if i register for a single element event will the SAX parse stops processing the XML message after encountering the element.
No.
Read in a book DOM reads the entire XML and loads into memory even if i want to know single element value.
Yes.
2 Comments
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.
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.
2 Comments
I'm not sure I grasp all the details of what you want to accomplish but if you have xsd files describing the XML format I'd say JAXB is the way to go. It'll take you xsd and generate classes for you all auto-magically. Then you can easily go back and forth - marshal and unmarshal data. I was very impressed by the level of automation in Netbeans recently when I tackled a similar task
Hope my suggestion to look into JAXB is helpful.
1 Comment
For simple XML to POJO, I've found XStream to be the least annoying of all parsers in Java:
Comments
DOM and SAX are interfaces, Xerces is the most popular implementation of these interfaces. There are in fact two versions of Xerces: the one from Apache, and the one built into the Sun/Oracle JDK. The one in the JDK is buggy; use the one from Apache in preference.
Since you are just starting out, it is probably worth looking more widely than DOM and SAX. SAX is a very low-level interface, offering good performance but little functionality: you can expect to write rather a lot of complex code in your application if this is the way you go. DOM presents you with a tree model of XML, but there are much better and simpler interfaces that do the same thing: JDOM offers everything you need and is much easier to use than DOM.