Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to Parse XML files in Java..

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
0
0
US
Hi,
I am trying to find out the best possible way to load and parse XML files in Java..I have heard of JAXP, JAXB etc..but is there an industry standard that every only is following?

pat
 
It depends on what you need. I've worked with jdom ( and xstream (
xstream is very fast and easy to use, but you must know the exact xml structure and create a java class that represents it. I found it more useful for serializing objects on disk than for xml parsing.

With jdom you parse or create a xml document with its childs, elements, etc... so it's a better solution if your xml document doesn't have a fixed structure.
 
Another option for parsing is SAX, but al MoreFeo said, all will depend on your needs.


Cheers,
Dian
 
Another thing,

dom and jdom parse the entire xml document in memory, so you can go back and forward thru the xml tree and nodes. (dom is a standard defined by W3C, and jdom is similar but defined more "friendly" with java).

xstream also loads the whole xml into memory, but instead of creating a "generix" xml object (a document with childs, nodes, text, etc...) it creates a java class that you must have defined. It's main goal is to serialize java objects into xml documents.

sax doesn't create the entire xml tree structure in memory, so it can be better for large xml files, using less memory. But you cannot process an element once you've read the next one.

dom, jdom, sax, etc... all have their own parser, and you cannot work with a document parsed by one of the them with the other parsers.

JAXP is not a parser, it's a set of classes that can be implemented in other parsers, so if you're using a JAXP supporting parser, you could change to another parser if it also supports JAXP.

JAXB is "similar" to xstream, it doesn't create a "generic" xml document in memory, it creates a specific java object that represents the xml structure. It uses xml schemas to map the elements of the xml document to attributes in the java class.

So once again, it depends on what you need.

I'm not familiar with all this xml stuff, but I think this can give you a starting point based on your needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top