JAXB将XSD转换为Java类 [英] JAXB converting XSD to Java classes
问题描述
当我运行以下命令:
xjc -b xmlSchema.xjb -d src -p com.q1labs.qa .xmlgenerator.model.generatedxmlclasses xmlSchema.xsd
它创建Java类,但我发现我的根类没有正确的名称,没有 @XmlRootElement
,它声明它作为根元素,这意味着当我使用类生成XML时,它没有正确形成。 / p>
XSD模式:
<?xml version =1.0 encoding =UTF-8?>
< xs:schema attributeFormDefault =unqualified
elementFormDefault =qualifiedtargetNamespace =http://ibm.org/seleniumframework
xmlns:xs =http:// www .w3.org / 2001 / XMLSchema>
< xs:element name =Testtype =sel:TestTypexmlns:sel =http://ibm.org/seleniumframework/&
< xs:complexType name =TestType>
< xs:choice minOccurs =1maxOccurs =unbounded>
< xs:element type =sel:Option1name =Option1xmlns:sel =http://ibm.org/seleniumframework/&
< xs:element type =sel:Option2name =Option2xmlns:sel =http://ibm.org/seleniumframework/&
< xs:element type =sel:Option3name =Option3xmlns:sel =http://ibm.org/seleniumframework/&
< / xs:choice>
< / xs:complexType>
这是我得到的输出:
<?xml version =1.0encoding =UTF-8standalone =yes?>
< testType xmlns =http://ibm.org/seleniumframework/>
生成的类对应于复杂类型。被声明为全局元素的一部分的匿名复杂类型将获得 @XmlRootElement
注释。其他人将在 ObjectFactory
类上生成 @XmlElementDecl
注释。这是因为可能有多个全局元素对应于相同的复杂类型。
详细信息
When I run the following command:
xjc -b xmlSchema.xjb -d src -p com.q1labs.qa.xmlgenerator.model.generatedxmlclasses xmlSchema.xsd
It creates Java classes however I've found that my root class does not have the correct name and does not have @XmlRootElement
which declares it as a root element which means when I use the classes to generate XML it is not formed properly.
XSD Schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" targetNamespace="http://ibm.org/seleniumframework"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Test" type="sel:TestType" xmlns:sel="http://ibm.org/seleniumframework"/>
<xs:complexType name="TestType">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element type="sel:Option1" name="Option1" xmlns:sel="http://ibm.org/seleniumframework"/>
<xs:element type="sel:Option2" name="Option2" xmlns:sel="http://ibm.org/seleniumframework"/>
<xs:element type="sel:Option3" name="Option3" xmlns:sel="http://ibm.org/seleniumframework"/>
</xs:choice>
</xs:complexType>
This is the output I am getting:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testType xmlns="http://ibm.org/seleniumframework"/>
Generated classes correspond to complex types. Anonymous complex types that are declared as part of global elements will get an @XmlRootElement
annotation. Others will have a @XmlElementDecl
annotation generated on the ObjectFactory
class. This is because there may be more than one global element that corresponds to the same complex type.
For More Information
这篇关于JAXB将XSD转换为Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!