tags:

views:

2376

answers:

7

Hi,

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?

+5  A: 

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.

basszero
+3  A: 

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

bruno conde
sadly you cannot validate against a schema with xstream ...
Karussell
Because then your Xstream annotations must be maintained to conform with the XSD. You redundant definitions of your schema.
+1  A: 

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.

Thomas Jones-Low
+1  A: 

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.

Mike Pone
+1  A: 

Take a look at JDOM.

dacracot
+1  A: 

Hi All,

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.

Lehane
A: 

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

Vihung