java - JAXB2.0 -XJC编译问题

标签 java xml

我正在使用 JAXB 2.0 生成 POJO 。 我有以下 XSD

<?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="test">
      <xs:complexType>
        <xs:attribute name="system" type="xs:string"/>
        <xs:attribute name="dim" type="xs:integer"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="test1" type="xs:string"/>
    <xs:element name="test2" type="xs:string"/>
    <xs:element name="scoring_guide" type="embedded_scoring_guide_type"/>
    <xs:complexType name="embedded_scoring_guide_type">
      <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="test"/>
          <xs:element ref="test1"/>
        </xs:choice>
        <xs:choice>
          <xs:element ref="test2" maxOccurs="unbounded"/>
        </xs:choice>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="test"/>
          <xs:element ref="test1"/>
        </xs:choice> 
      </xs:sequence>
    </xs:complexType>
</xs:schema>

当我尝试从上面的 Schema 生成 POJO 时。我收到以下错误:

Gennerating jaxb objects for C:\WORKAREA\JAXB\SubSeqenceElementCall\schema\SubSequentElementCall.xsd Using XJC generator...
parsing a schema...

[ERROR] Property "TestOrTest1" is already defined. Use &lt;jaxb:property> to resolve this conflict.
  line 14 of file:/C:/WORKAREA/JAXB/SubSeqenceElementCall/schema/SubSequentElementCall.xsd

[ERROR] The following location is relevant to the above error
  line 21 of file:/C:/WORKAREA/JAXB/SubSeqenceElementCall/schema/SubSequentElementCall.xsd

[ERROR] Element "test1" shows up in more than one properties.
  line 21 of file:/C:/WORKAREA/JAXB/SubSeqenceElementCall/schema/SubSequentElementCall.xsd

[ERROR] The following location is relevant to the above error
  line 14 of file:/C:/WORKAREA/JAXB/SubSeqenceElementCall/schema/SubSequentElementCall.xsd

Failed to parse a schema.
Gennerating jaxb objects for C:\WORKAREA\JAXB\SubSeqenceElementCall\schema\SubSequentElementCall.xsd Completed!

请帮忙解决上述问题。

谢谢 维克拉姆

最佳答案

尝试定义 JAXB 内联属性或绑定(bind)文件,或避免使用相同字段的匿名序列/选择。

这是解决您的问题的 JAXB 内联绑定(bind)示例:

<?xml version="1.0" encoding="UTF-8"?>
  <xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0">
    <xs:element name="test">
      <xs:complexType>
        <xs:attribute name="system" type="xs:string"/>
        <xs:attribute name="dim" type="xs:integer"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="test1" type="xs:string"/>
    <xs:element name="test2" type="xs:string"/>
    <xs:element name="scoring_guide" type="embedded_scoring_guide_type"/>
    <xs:complexType name="embedded_scoring_guide_type">
      <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <!-- Additional JAXB annotations -->
            <xs:annotation>
                <xs:appinfo>
                   <jxb:property name="firstBlock"/>
                </xs:appinfo>
            </xs:annotation>
          <xs:element ref="test"/>
          <xs:element ref="test1"/>                    
        </xs:choice>
        <xs:choice>
          <xs:element ref="test2" maxOccurs="unbounded"/>
        </xs:choice>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <!-- Additional JAXB annotations -->
            <xs:annotation>
                <xs:appinfo>
                   <jxb:property name="secondBlock"/>
                </xs:appinfo>
            </xs:annotation>                  
          <xs:element ref="test"/>
          <xs:element ref="test1"/>
        </xs:choice> 
      </xs:sequence>
    </xs:complexType>
</xs:schema>

另请参阅 Oracle 文档 http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html

关于java - JAXB2.0 -XJC编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8792816/

相关文章:

c# - 如何解析 xml 并从字符串中循环遍历它?

xml - 格式化时省略 XML 实体

java - if 或语句 xml 和 java 控制台输出

xml - xslt 中文化特定值的总和

java - 为什么 JDOM 的 getChild() 方法返回 null?

java - java http 客户端如何验证服务器的 CA 证书?

java - 在 android Studio 中,我希望当我单击按钮时,下一个 Activity/fragment 应该来自右侧

Java Trident 库和 Thread.sleep()

java - 可以生成多少个唯一 ID

java - 如果类存在,则调用构造函数