Question

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!

Was it helpful?

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>

OTHER TIPS

The other answer doesn't use the collection attribute which I think is probably what you're ultimately needing.

Something like this might work when included in your mapping for the Order object:

<field name="items" type="item" collection="arraylist" >
  <bind-xml name="items" node="element"/>
</field>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top