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
Does not generate @XMLRootElement jaxb
提问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 @XMLRootElement
will 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 @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
.
对于对应于命名为复杂类型的全局元素的@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 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.
创建JAXBContext
基于从 XML 模式生成的模型时,应该在生成的模型的包名称上完成。这是为了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
back实例。
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
被返回,你总是可以JAXBIntrospector
在解组操作的结果上使用:
PersonType pt = (PersonType) JAXBIntrospector.getValue(unmarshaller.unmarshal(xml));
For More Information
想要查询更多的信息