JAXB xjc:简单绑定不会为基类生成@XmlRootElement [英] JAXB xjc:simple binding does not generate @XmlRootElement for base class
问题描述
我正在使用JAXB 2.0。我在XSD文件中定义了各种元素和类型。这是一个例子:
I am using JAXB 2.0. I have various elements and types defined in an XSD file. Here's an example:
<xs:element name="Person" type="Person" />
<xs:complexType name="Person">
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
<xs:element name="Musician" type="Musician"/>
<xs:complexType name="Musician">
<xs:complexContent>
<xs:extension base="Person">
<xs:attribute name="instrument" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="People" type="People"/>
<xs:complexType name="People">
<xs:sequence>
<xs:element name="person" type="Person" minOccurs="0" maxOccurs="Unbounded/>
</xs:sequence>
</xs:complexType>
从上面的架构示例中可以看出,我们有一个Person,一个名字,一个音乐家,也是一个人(虽然这可能会受到一些争论,但那是另一个论坛)。还有一个People元素,它本质上是Person类型的集合。
So as you can see from the above schema example, we have a Person, who has a name, and a Musician, who is also a Person (though this may be subject to some debate, but that's for another forum). There is also a People element, which is essentially a collection of Person types.
我在绑定文件中有以下内容:
I have the following in a bindings file:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0"
xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings optionalProperty="wrapper">
<xjc:simple/>
</jaxb:globalBindings>
我的意图d使用这些对象是:
My intended use of these objects is:
- 一个人可能看起来像一个单独的Marshalled元素,或者可能是People对象的一部分
- 如果Person是People对象的一部分,那么xsi:type应该表明它只是普通人还是音乐家。
所以我需要生成的Java类包含@XmlRootElement注释和@XmlType注释。 xjc:simple绑定为音乐家创建两个注释,但只为Person创建@XmlType。因此,当我对一个Person对象进行Marshall时,我得到的只是:
So I need the generated Java classes to contain both an @XmlRootElement annotation as well as an @XmlType annotation. The xjc:simple binding creates both annotations for a Musician, but only creates the @XmlType for Person. So when I Marshall a Person object, all I get is:
<?xml version="1.0" encoding="UTF-8"?>
我希望看到的是:
<?xml version="1.0" encoding="UTF-8"?>
<Person name="John Doe"/>
对于People对象,我想看:
For a People object, I want to see:
<?xml version="1.0" encoding="UTF-8"?>
<People>
<person name="John Doe" xsi:type="Person"/>
<person name="Keith Richards" xsi:type="Musician"/>
</People>
我读过关于xjc的简单绑定,它适用于继承中的所有最低级别层次结构。但是,基类最终没有@XmlRootElement注释。对于我正在研究的用例,必须将基类作为顶级元素和其他元素的成员进行编组。任何建议都是受欢迎的。
I have read about the simple binding with xjc, and it works with all the lowest levels in an inheritance hierarchy. However, the base classes end up without an @XmlRootElement annotation. For the use case I'm working on, it's imperative that base classes can be Marshalled as both a top-level element and as a member of other elements. Any suggestions would be welcome.
推荐答案
https://github.com/highsource/jaxb2-annotate-plugin
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:annox="http://annox.dev.java.net"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc annox">
<jaxb:bindings node="//xs:complexType[@name='Person']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="person"/>
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
这篇关于JAXB xjc:简单绑定不会为基类生成@XmlRootElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!