不生成@XMLRootElement jaxb [duplicate]

[英]Does not generate @XMLRootElement jaxb [duplicate]


This question already has an answer here:

这个问题已经有了答案:

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。任何一个人都能给出一个解决方案。我知道可以生成根元素,但这不行。

2 个解决方案

#1


6  

For global elements corresponding to named complex types an @XmlElementDecl annotation on the ObjectFactory class will be generated instead of an @XmlRootElement annotation 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.

对于与命名复杂类型对应的全局元素,将生成ObjectFactory类上的@XmlElementDecl注释,而不是类上的@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 JAXBContext based 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 ObjectFactory class gets processed.

当基于从XML模式生成的模型创建JAXBContext时,应该在生成的模型的包名上执行。这使得ObjectFactory类中的元数据得到处理。

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

Or the generated ObjectFactory class:

或生成的ObjectFactory类:

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

Unmarshalling the Class

数据编出类

When you unmarshal a class in which the root element corresponds to an @XmlElementDecl annotation you will get an instance of JAXBElement back.

当解封根元素对应于@XmlElementDecl注释的类时,将得到JAXBElement实例。

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

If you want to guard against a JAXBElement being returned you can always use the JAXBIntrospector on the result of the unmarshal operation:

如果您想防止JAXBElement被返回,您可以对unmarshal操作的结果使用JAXBIntrospector:

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

For More Information

的更多信息

#2


1  

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

@XMLRootElement只会为匿名类型的顶级元素生成,而不是顶级类型。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/11/11/3a696ce5687600a90daaed99b197a25a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号