1

We have been using JAXB to generate XML to interface with a third party. This third party is asking that for one section we produce a set of 2 different 0-n XML Elements in a repeating fashion without parent-elements separating them. Here is an example of whats requested:

<education>
   <code>ENG24</code>
   <percentage>25</percentage>
   <code>ENG25</code>
   <percentage>20</percentage>
   <code>SPA50</code>
   <percentage>30</percentage>
   <code>SPA60</code>
   <percentage>25</percentage>
</education>

I cannot figure out a way to represent this type of XML with JAXB Java XML Binding. Is it at all possible to represent the above XML with JAXB Java XML Binding?

I am aware that the XML above is poorly designed but I cannot change the third party's mind to use and tags instead.

If JAXB XML binding is not going to work that I would be very thankful for suggestions of what library/tool to use instead to produce XML and to do the marshal/un-marshaling.

Thanks! Matt

lexicore
  • 42,748
  • 17
  • 132
  • 221
Matt
  • 21
  • 2

1 Answers1

0

Yes, it is possible. The simplest would be probably to use @XmlElements/@XmlElement combo:

@XmlElements({
    @XmlElement(name="code", type=String.class),
    @XmlElement(name="percentage", type=Integer.class)
})
public List<Serializable> items;

Alternatively you can also use @XmlElementRef/@XmlElementRef and have a List<JAXBElement<? extends Serializable>> items. Each item would be then a JAXBElement<? extends Serializable> carrying the value as well as name of the element.

But since you seem to have different types (String/Integer), @XmlElements/@XmlElement should work as well and is much easier to use.

lexicore
  • 42,748
  • 17
  • 132
  • 221