I need to parse an XML file in java, (I am using DOM) that built a tree, and then I need to create objec开发者_StackOverflow中文版ts using nodes (and their attributes). Is this a good way of approach? Can i still do the same thing without, SAX, or DOM?
JAXB is the standard way to create objects from XML.
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.
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.)
精彩评论