使用JAXB创建类时出错

时间:2013-11-21 09:33:21

标签: java xml jaxb

我第一次使用JAXB并尝试使用我的xsd文件创建类文件,但遗憾的是会出错。

这是我的xsd:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="DbOperation" type="DbOperation"/>

  <xs:complexType name="DbOperation">
    <xs:sequence>
      <xs:element name="ruleList" minOccurs="0">
    <xs:complexType>
    <xs:sequence>
        <xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
      </xs:element> 
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="Rule">
    <xs:sequence>
      <xs:element name="Action" type="xs:string" minOccurs="0"/>
      <xs:element name="TableName" type="xs:string" minOccurs="0"/>
      <xs:element name="conditionList" minOccurs="0">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Conditions" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
      </xs:element> 
     </xs:sequence>
  </xs:complexType>

  <xs:complexType name="Conditions">
    <xs:sequence>
      <xs:element name="Condition" type="Condition" minOccurs="0"/>
      <xs:element name="Operator" type="xs:string" minOccurs="0"/>
      <xs:element name="Condition" type="Condition" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType> 

  <xs:complexType name="Condition">
    <xs:sequence>
      <xs:element name="fieldName" type="xs:string" minOccurs="0"/>
      <xs:element name="Operation" type="xs:string" minOccurs="0"/>
      <xs:element name="Value" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

这些是我得到的错误: 解析模式......

[ERROR] src-resolve: Cannot resolve the name 'Rule' to a(n) 'element declaration
' component.
  line 11 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd
[ERROR] src-resolve: Cannot resolve the name 'Conditions' to a(n) 'element decla
ration' component.
  line 25 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd
[ERROR] cos-nonambig: Condition and Condition (or elements from their substituti
on group) violate "Unique Particle Attribution". During validation against this
schema, ambiguity would be created for those two particles.
  line 32 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd

无法解析架构。

这是我为其创建了xsd:

的示例xml
<?xml version="1.0" encoding="UTF-8"?>
<DbOperation>

      <Rule>
            <Action>Delete</Action>
            <TableName>
                  issue
            </TableName>
            <Conditions>
                  <Condition>
                        <fieldName>
                trader
                        </fieldName>
                        <Operation>
                >=
                        </Operation>
                        <Value>
                250
                        </Value>
                  </Condition>
                  <Operator>AND</Operator>

                  <Condition>
                        <fieldName>
                traderName
                        </fieldName>
                        <Operation>
                =
                        </Operation>
                        <Value>
                jk
                        </Value>
                  </Condition>
            </Conditions>
      </Rule>

</DbOperation>

2 个答案:

答案 0 :(得分:0)

使用XML编辑器首先验证您的架构......

您正在引用一个类型,就好像它是一个元素

  <xs:element ref="Conditions" minOccurs="0" maxOccurs="unbounded"/>

  <xs:complexType name="Conditions">
    <xs:sequence> ....

答案 1 :(得分:0)

<xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>

“REF”属性表示元素不是类型

可以通过两种方式纠正错误:

第一

<xs:complexType name="DbOperation">
    <xs:sequence>
      <xs:element name="ruleList" minOccurs="0">
         <xs:complexType>
            <xs:sequence>
               <xs:element name="Rule" type="Rule" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
         </xs:complexType>
      </xs:element> 
    </xs:sequence>
  </xs:complexType>

第二

<xs:complexType name="DbOperation">
    <xs:sequence>
      <xs:element name="ruleList" minOccurs="0">
         <xs:complexType>
            <xs:sequence>
               <xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
         </xs:complexType>
      </xs:element> 
    </xs:sequence>
  </xs:complexType>

<xs:element name="Rule" type="Rule"/>