38

I was wondering what the best or most widely used apis are to convert a java object to xml. I'm fairly new on the subject. Is there some sort of api call you can make to pass in an object and return xml? Or is it much more tedious where as you need to construct the document manually by pulling out object values?

I have been reading about xerces, sax, and jaxb. I would like to continue along this open source route.

Thanks!

Atma
  • 29,141
  • 56
  • 198
  • 299

6 Answers6

55

JAXB is definitely the solution.

Why? Well, it's inside the JDK 6, so you'll never find it unmaintained.

It uses Java annotations to declare XML-related properties for classes, methods and fields.

Tutorial 1

Tutorial 2

Note: JAXB also enables you to easily 'unmarshal' XML data (which was previously marshalled from Java object instances) back to object instances.

One more great thing about JAXB is: It is supported by other Java-related technologies, such as JAX-RS (a Java RESTful API, which is availible as part of Java EE 6). JAX-RS can serve and receive JAXB objects on the fly, without the need of marshalling/unmarshalling them. You might want to check out Netbeans, which contains out-of-the-box support for JAX-RS. Read this tutorial for getting started.

edit:

To marshall/unmarshall 'random' (or foreign) Java objects, JAXB offers fairly simple possibility: One can declare an XmlAdapter and 'wrap' existing Java classes to be JAXB-compatible. Usage of such XmlAdapter is done by using the @XmlJavaTypeAdapter-annotation.

Nick
  • 2,827
  • 4
  • 29
  • 39
ivan_ivanovich_ivanoff
  • 19,113
  • 27
  • 81
  • 100
  • 1
    This package seems geared around mapping an existing XML file to java code, rather than quick and dirty persistence of "random" java objects. ... which is a useful thing to do, but not quite what was asked for, I think. – Roboprog Apr 10 '09 at 01:21
  • please see the edit section of my answer – ivan_ivanovich_ivanoff Apr 10 '09 at 01:25
  • 1
    JAXB is for binding, not serialization. Use java.beans.XMLEncoder for simple serialization. – erickson Apr 10 '09 at 05:47
  • Also JAXB is a spec so there are multiple implementations: Metro (http://jaxb.dev.java.net/), MOXy (http://www.eclipse.org/eclipselink/moxy.php), etc. – bdoughan Sep 07 '10 at 16:23
  • @ivan_ivanovich_ivanoff Hi, your `Tutorial 2` link seems to be broken. Can you kindly fix? Thanks – aggregate1166877 Nov 01 '12 at 13:01
  • It might look out of context, but is there any way to do it without third party APIs? – AKS Sep 09 '13 at 14:43
  • JAXB is not necessarily **the** solution for everyone. Other libraries are easier in some cases, for example turning any old object into XML without having to modify it first by adding annotations. – AaronJ Mar 19 '14 at 17:12
19

You might want to look at XStream: http://x-stream.github.io

james.garriss
  • 12,959
  • 7
  • 83
  • 96
Gary Kephart
  • 4,860
  • 5
  • 39
  • 52
4

Available with Java 6 is an API to convert annotated Java Objects to XML. The following code shows how to convert an annotated object to an XML string

final JAXBElement<Type> o = new ObjectFactory().createElement(new Type());

final Marshaller m = JAXBContext.newInstance(Type.class).createMarshaller();

// Do this if you want the result to be more human readable.
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(o, System.out);

You can further extend this by adding schema validation (note very slow, but useful for debugging)

final JAXBElement<Type> o = new ObjectFactory().createElement(new Type());

final Marshaller m = JAXBContext.newInstance(Type.class).createMarshaller();
final Schema schema = SchemaFactory.newInstance(
  "http://www.w3.org/2001/XMLSchema").newSchema(
  getClass().getResource("/META-INF/wsdl/schema.xsd"));
m.setSchema(schema);
m.marshal(o, System.out);

You don't need to do a type conversion to JAXBElement if Type is a defined element. (i.e. has an annotation @XmlRootElement)

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
3

For anyone who decides to use JAXB, please make sure to cache JAXBContext. JAXBContext.newInstance is known to be very expensive. I documented a case where the conversion to xml is used in logging and JAXBContext.newInstance is not cached, it caused performance overhead up to 5seconds when invoked by 20 concurrent users.

http://app-inf.blogspot.com/2012/10/performance-tuning-logging-right-way.html

This is not to discourage anyone using JAXB. Once JAXBContext is cached, the performance is excellent, as discussed in the above link.

1

XMLBeans is another one, similar to JAXB. I haven't looked at JAXB in a while, when I did it was fairly bad compared to XMLBeans, but that was years ago (and I prefer to use things that are in the JDK as opposed to 3rd party ones, but I still use XMLBeans to this day).

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
1

There are many open source frameworks in this space. However, Simple as its name suggests, is by far the easiest way to perform serialization. Take a look at the Tutorial. Another feature is that it can perform polymorphic serialization, which means its not as constrained as JAXB for example.

ng.
  • 7,099
  • 1
  • 38
  • 42