In J开发者_如何转开发ava for XML marshalling/unmarshalling one can use JAXB, JIBX, CASTOR, ADB etc. But out of these which is most generic & commonly used? Or is there any other utility available?
The standard is the JAXB (JSR 222), and the famous project with this name is the reference implementation. Unlike JAXB 1.0, JAXB 2.0 RI is quite good, and I've used it a lot. Other libraries implements the JAXB standard (I think tha Castor and JiBX, but I have no experience with them).
I've also use XStream, which was very easy and simple - it has a proprietary API though.
I don't know of any benchmark other than https://bindmark.dev.java.net/old-index.html - notice it is a 4 year old one. perhaps you can take it's ideas or any usable code it may have and run some tests yourself.
I personally use XMLBeans. I like it more than JAXB (and I am biased in favour of things from Sun since they should be more "standard"). I cannot speak to the commonly used part of it though.
JAXB is preferred because of following reasons:
- No extra jaxb libraries are required if you are using JDK1.6 or above, because JAXB is bundled in JDK 1.6.
- The
jaxbMarshaller.unmarshal()
contains a lot of overloaded methods, find one that suit yours. - JAXB is pretty easy to understand and operate. what it requires is couple of annotations like
@XmlRootElement
and@XmlAccessorType
along with four lines of code - Modifying XML files in Java is much easier using JAXB as compared to other XML parsers like DOM and SAX because you only deal with POJOs
- JAXB is more memory efficient than DOM or SAX parser.
- JAXB doesn't require XML schema to work. Though you can use XML Schema for generating corresponding Java classes and its pretty useful if you have large and complex XML Schema which will result in huge number of classes but for simple usage you can just annotate your object with relevant XML annotations provided by JAXB package i.e.
java.xml.binding
and you are good to go.
I use CASTOR mapping and IMHO it is very easy to use provided you are familiar with XML. All you need to do is to create Java objects and map them to the XMl using a mapping file. The mapping file is required if your XML is a complex one. If it is a simple and straight forward XML, CASTOR will itself look for respective Java class which matches the XML element name [Castor uses Java's Reflection APIs to do this].
精彩评论