Lehane
Lehane

Reputation: 48658

Java object to XML schema

If you have a Java object and an XML schema (XSD), what is the best way to take that object and convert it into an xml file in line with the schema. The object and the schema do not know about each other (in that the java classes weren't created from the schema).

For example, in the class, there may be an integer field 'totalCountValue' which would correspond to an element called 'countTotal' in the xsd file. Is there a way of creating a mapping that will say "if the object contains an int totalCountValue, create an element called 'countTotal' and put it into the XML". Similarly, there may be a field in the object that should be ignored, or a list in the object that should correspond to multiple XML elements.

I looked at XStream, but didn't see any (obvious) way of doing it. Are there other XML libraries that can simplify this task?

Upvotes: 3

Views: 9500

Answers (7)

Vihung
Vihung

Reputation: 13397

I would say JAXB or Castor. I have found Castor to be easier to use and more reliable, but JAXB is the standard

Upvotes: 0

Lehane
Lehane

Reputation: 48658

I tried out most of the libraries suggested in order to see which one is most suited to my needs. I also tried out a library that was not mentioned here, but suggested by a colleague, which was a StAX implementation called Woodstox.

Admittedly my testing was not complete for all of these libraries, but for the purpose mentioned in the question, I found Woodstox to be the best. It is fastest for marshalling (in my testing, beating XStream by around 30~40%). It is also fairly easy to use and control.

The drawback to this approach is that the XML created (since it is defined by me) needs to be run through a validator to ensure that it is correct with the schema.

Upvotes: 2

dacracot
dacracot

Reputation: 22338

Take a look at JDOM.

Upvotes: 1

Mike Pone
Mike Pone

Reputation: 19320

You can use a library from Apache Commons called Betwixt. It can map a bean to XML and then back again if you need to round trip.

Upvotes: 1

Thomas Jones-Low
Thomas Jones-Low

Reputation: 7161

I use a java library called JiBx to do this work. You need to write a mapping file (in XML) to describe how you want the XML Schema elements to map to the java objects. There are a couple of generator tools to help with automating the process. Plus it's really fast.

Upvotes: 2

bruno conde
bruno conde

Reputation: 48255

I'm doing Object do XML serialization with XStream. What don't you find "obvious" with this serializer? Once you get the hang of it its very simple.

In the example you provided you could have something like this:

...
XStream xstream = new XStream(new DomDriver());

xstream.alias("myclass", MyClass.class);
xstream.aliasField("countTotal", MyClass.class, "totalCountValue");

String xml = xstream.toXML(this);
...

for this sample class:

class MyClass {
     private int totalCountValue;

     public MyClass() {
     }
} 

If you find some serializer more simple or "cool" than this please share it with us. I'm also looking for a change...

Check the XStream mini tutorial here

Upvotes: 4

basszero
basszero

Reputation: 30004

I believe this can be achieved via JAXB using it's annotations. I've usually found it much easier to generate Objects from JAXB ( as defined in your schema) using XJC than to map an existing Java object to match my schema. YMMV.

Upvotes: 6

Related Questions