1

I need to parse an XML file in java, (I am using DOM) that built a tree, and then I need to create objects using nodes (and their attributes). Is this a good way of approach? Can i still do the same thing without, SAX, or DOM?

3 Answers 3

3

JAXB is the standard way to create objects from XML.

Sign up to request clarification or add additional context in comments.

7 Comments

That's right. The javax.xml.bind.* packages are part of JDK 1.6 and above.
JAXB is great as long as you've got complete control over the sources (i.e. can annotate it accordingly), if not Sax/StAX are your next best options.
sources mean XML files right? yeah i have complete control over them. I have the schema file(*xsd) and the XML file(*apx). btw i was given the files with those extensions, but told they are xml files. what apx and xsd stand for?
In the interest of full disclosure, Blaise Doughan is the team lead for JAXB.
@duffymo - I lead EclipseLink MOXy which implements JAXB 2 (JSR-222). There are other implementations of the JAXB standard including Metro (the reference implementation) and Apache JaxMe.
|
1

SAX and DOM are the two standard parsers for XML. SAX does not create a tree in-memory for you; it fires off events that you can respond to according to your needs. DOM takes the additional step of parsing the XML and creating an in-memory tree for you that you can manipulate using a standard API.

If you don't want either SAX or DOM, it means writing your own parser. Why on earth would you want to do that?

JAXB, the standard way to create objects from XML, is re-using the standard parser under the covers.

3 Comments

StAX (JSR-173) and JAXB (JSR-222) are also Java standards for processing XML.
"is re-using the standard parser" which one?
@pantelis - I think the point that duffymo was trying to make is that JAXB implementations make use of lower level StAX or SAX parser implementations (although it is not a requirement).
0

There are a number of tree models for XML that are architecturally similar to DOM, but with better performance and better usability: my preference is XOM, others are JDOM and DOM4J. If you do need to use this level of interface, DOM is a very inferior choice: no-one would use it if it didn't happen to be the only one that made it into the JDK. (But my personal preference, whenever possible, is to avoid low-level conversion of XML data to Java objects, and instead to do all the processing in higher-level XML-oriented languages such as XSLT and XQuery.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.