Java 不生成@XMLRootElement jaxb

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19911772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:14:47  来源:igfitidea点击:

Does not generate @XMLRootElement jaxb

javaxmljaxbxsd

提问by Sithira Pathmila

This is my .xsd file

这是我的 .xsd 文件

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person" type="PersonType"/>
    <xs:complexType name="PersonType">

        <xs:sequence>
            <xs:element name="Name" type="xs:string"/>
            <xs:element name="Address" type="AddressType" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="AddressType">

        <xs:sequence>
            <xs:element name="Number" type="xs:unsignedInt"/>
            <xs:element name="Street" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

using this XSD file I Generated this class :

使用这个 XSD 文件我生成了这个类:

package demo5;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PersonType", propOrder = {
  "name",
  "address"
})
public class PersonType {

@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Address", required = true)
protected List<AddressType> address;

public String getName() {
    return name;
}

public void setName(String value) {
    this.name = value;
}
public List<AddressType> getAddress() {
    if (address == null) {
        address = new ArrayList<AddressType>();
    }
    return this.address;
}

}

but XSD file does not generates the @XMLRootElement in the java file. any one can give a solution for this. I know can generate the root element but this does not work.

但是 XSD 文件不会在 java 文件中生成 @XMLRootElement。任何人都可以为此提供解决方案。我知道可以生成根元素,但这不起作用。

回答by Puce

The @XMLRootElementwill only be generated for anonymous types of top elements, not top-level types.

@XMLRootElement将仅针对匿名类型顶端元件,而不是顶级类型的产生。

回答by bdoughan

For global elements corresponding to named complex types an @XmlElementDeclannotation on the ObjectFactoryclass will be generated instead of an @XmlRootElementannotation on the class. This is because there may be more than one global element corresponding to the same named complex type. This use case could not be met by using @XmlRootElement.

对于对应于命名为复杂类型的全局元素的@XmlElementDecl关于该注解ObjectFactory类将被产生,而不是@XmlRootElement在类注释。这是因为可能有多个全局元素对应于同一个命名的复杂类型。使用 无法满足此用例@XmlRootElement

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name="Person")
    public JAXBElement<PersonType> createPerson(PersonType personType) {
        return new JAXBElement<PersonType>(new QName("Person"), PersonType.class, personType);
    }

}

Creating the JAXBContext

创建 JAXBContext

When creating a JAXBContextbased on a model generated from an XML Schema it should be done on the package name of the generated model. This is so the metadata in the ObjectFactoryclass gets processed.

创建JAXBContext基于从 XML 模式生成的模型时,应该在生成的模型的包名称上完成。这是为了ObjectFactory处理类中的元数据。

JAXBContext jc = JAXBContext.newInstance("demo5");

Or the generated ObjectFactoryclass:

或者生成的ObjectFactory类:

JAXBContext jc = JAXBContext.newInstance(demo5.ObjectFactory.class);

Unmarshalling the Class

解组类

When you unmarshal a class in which the root element corresponds to an @XmlElementDeclannotation you will get an instance of JAXBElementback.

当您解组根元素对应于@XmlElementDecl注释的类时,您将获得一个JAXBElementback实例。

JAXBElement<PersonType> je = (JAXBElement<PersonType>) unmarshaller.unmarshal(xml);
PersonType pt = je.getValue();

If you want to guard against a JAXBElementbeing returned you can always use the JAXBIntrospectoron the result of the unmarshal operation:

如果你想防止JAXBElement被返回,你总是可以JAXBIntrospector在解组操作的结果上使用:

PersonType pt = (PersonType) JAXBIntrospector.getValue(unmarshaller.unmarshal(xml));

For More Information

想要查询更多的信息