简体   繁体   English

JAXB XML在没有父元素的情况下重复交替XmlElements

[英]JAXB XML repeating alternating XmlElements without Parent element

We have been using JAXB to generate XML to interface with a third party. 我们一直在使用JAXB生成XML以与第三方接口。 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. 该第三方要求在一个部分中,我们以重复的方式生成一组2个不同的0-n XML元素,而没有父元素将它们分开。 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. 我无法找到一种用JAXB Java XML绑定表示这种XML的方法。 Is it at all possible to represent the above XML with JAXB Java XML Binding? 是否可以用JAXB Java XML绑定表示上述XML?

I am aware that the XML above is poorly designed but I cannot change the third party's mind to use and tags instead. 我知道上面的XML设计不当,但是我不能改变使用和标记的第三方想法。

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. 如果JAXB XML绑定无法正常工作,那么我将非常感谢您建议使用哪种库/工具来生成XML并进行编组/非编组。

Thanks! 谢谢! Matt 马特

Yes, it is possible. 对的,这是可能的。 The simplest would be probably to use @XmlElements / @XmlElement combo: 最简单的方法可能是使用@XmlElements / @XmlElement组合:

@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 或者,您也可以使用@XmlElementRef / @XmlElementRef并具有List<JAXBElement<? extends Serializable>> items List<JAXBElement<? extends Serializable>> items . List<JAXBElement<? extends Serializable>> items Each item would be then a JAXBElement<? extends Serializable> 每个项目将是一个JAXBElement<? extends Serializable> JAXBElement<? extends Serializable> carrying the value as well as name of the element. JAXBElement<? extends Serializable>携带元素的值和名称。

But since you seem to have different types ( String / Integer ), @XmlElements / @XmlElement should work as well and is much easier to use. 但是由于您似乎具有不同的类型( String / Integer ),所以@XmlElements / @XmlElement应该也可以正常工作,并且更易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM