当我运行以下命令时:
xjc -b xmlSchema.xjb -d src -p com.q1labs.qa.xmlgenerator.model.generatedxmlclasses xmlSchema.xsd
它创建了 Java 类,但是我发现我的根类没有正确的名称并且没有 @XmlRootElement
将其声明为根元素,这意味着当我使用这些类时生成格式不正确的 XML。
XSD 架构:
<?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>
这是我得到的输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testType xmlns="http://ibm.org/seleniumframework"/>
最佳答案
生成的类对应复杂类型。声明为全局元素一部分的匿名复杂类型将获得一个 @XmlRootElement
注释。其他人将在 ObjectFactory
类上生成一个 @XmlElementDecl
注释。这是因为可能有不止一个全局元素对应同一个复杂类型。
了解更多信息
关于java - JAXB 将 XSD 转换为 Java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12143838/