开发者

How to convert multiple POJOs to xml in required format?

开发者 https://www.devze.com 2023-04-12 15:26 出处:网络
We\'ve requirement where we need to generate XML file for sending it to another system. I was l开发者_如何转开发ooking at this example (which uses JAXB).

We've requirement where we need to generate XML file for sending it to another system. I was l开发者_如何转开发ooking at this example (which uses JAXB).

http://thomassundberg.wordpress.com/2010/01/19/how-to-convert-a-pojo-to-xml-with-jaxb/

However, this one converts only single POJO to xml whereas we have multiple POJOs e.g. Car/Wheel/Seats etc. that should appear in xml.

Any idea on how this can be achieved?

e.g. I would like to have something like this as final output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<car registration="abc123">
    <brand>Volvo</brand>
    <description>Sedan</description>

    <wheel>
      <brand>Ceat</brand>     
    </wheel>
</car>


It sounds like you're trying to convert a graph of Java objects to XML, which JAXB can do. Each object in the graph should have JAXB annotations. See http://numberformat.wordpress.com/2009/11/01/using-jaxb-to-convert-between-xml-and-pojos/.


If you have multiple instances of the same POJO class, you can create a List of them and convert that to XML, remembering that you need to wrap the list in a root element.

If you have multiple POJO classes then you can create a wrapper class to hold them and convert that to XML.


Castor to the rescue.

The link used to be to castor.org. That site appears to have gone away. Link updated compliments of Matt Ball (see comments below).

Castor Intro Info

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号