Search code examples
javaxmlxml-serializationcastoroxm

Mapping collection to XML in Castor


I'm trying to map a POJO to XML using Castor.

Let's say I have a Order that has a collection of Items... is there any way of achieving an xml like the following:

<order>
  ...order attributes
  <items>
    <item> ..item attributes </item>
    <item> ..other item </item>
  </items>
</order>

I could make something similar but without the <items> node. This wouldn't be a problem in other case but my XML must adhere to a strict XSD schema so I need to do it like that.

Thanks!


I though of a kind of "workaround" that would involve creating a new java object (that would be the node) that would only contain the list of items... can anyone think of a better approach? there's a 100 rep bounty open since now!


Solution

  • You can use the location attribute of the bind-xml lement

    http://castor.codehaus.org/1.2/xml-mapping.html#6.-Location-attribute

    Example from the docs:

       <class name="Foo">
          <field name="bar" type="Bar">
             <bind-xml name="bar" location="abc"/>
          </field>
       </class>
    

    Produces the following XML:

    <foo>;
       <abc>
          <bar>...</bar>
       </abc>
    </foo>