Wednesday, November 5, 2008

More Java XML

Adding to my previous post about Java XML.

Take the example where you want to take an existing class and customize your own XML output. You could use XStream out of the box without any configuration and just trust it to create the correct XML. This is fine if you want the object to be fully represented in the XML, but what if you only want a subset of the data in the object?

Well, XStream allows you to write custom converters for this. So, you need to create a converter class (that implements the Converter interface), and in the marshal() method, take the fields that you desire and manually create the XML nodes. This gives you much more control over the output.

Another option is to use StAX to directly map the input elements to the output XML. Again this gives you full control as to what is going into the actual XML document.

I ran a few tests to determine the performance difference between Woodstox (a StAX implementation) and XStream. Basically the test involved both libraries marshalling an object into XML data 100000 times. The difference was quite large, with Woodstox outperforming XStream by about 30~40%.

1 comment:

Unknown said...

Hi,
I saw your question in one mailing list about converting a java object to an XML file ( considering an XDS file) Have you found any suitable solution for that ? I've the same problem