愤怒的JAXB Parser用于有效的XML?

时间:2013-08-30 00:56:54

标签: java xml jaxb unmarshalling

我有以下XML:

<paths>
  <edge from='l2' to='l0'>
    <path>
        <NTStep symbol='XferObjOut' reverse='false' from='l2' to='o2040'>
          <NTStep symbol='FlowsTo' reverse='false' from='o2053' to='v1033' index='430'>
              <asgnRef reverse='false' from='v923' to='v924'/>
            <paramRef reverse='false' from='v924' to='v1033' index='326'/>
          </NTStep>
          <asgnRef reverse='false' from='v1033' to='v1032'/>
        </NTStep>
    </path>
  </edge>
</paths>

让JAXB全部陷入困境:

DefaultValidationEventHandler: [FATAL_ERROR]: cvc-complex-type.2.4.d: Invalid content was found starting with element 'Step'. No child element '{step}' is expected at this point. 
     Location: line 10 of file:/Users/gestalt/Desktop/test2/new.xml
Exception in thread "main" javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'Step'. No child element '{step}' is expected at this point.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:505)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:206)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
    at Test.main(Test.java:28)
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'Step'. No child element '{step}' is expected at this point.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)

...然而

xmllint验证xml / schema没有问题,并且相同的代码似乎可以在另一个系统上运行。

此外,如果删除了任何一条aRef或pRef行,则不会发生错误......

架构如下。我为这个长度道歉,但我对这个行业不熟悉,因此我更喜欢完整性和健全性......

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       elementFormDefault="qualified"
       jxb:version="2.1">

<!-- SIMPLE TYPES ==========================================================-->

<xs:simpleType name="NodeName">
  <xs:restriction base="xs:token">
    <xs:pattern value="v[0-9]+|u[0-9]+|l[0-9]+|o[0-9]+|f[0-9]+"/>
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="GrammarNTSymbol">
  <xs:restriction base="xs:token">
    <xs:enumeration value="ArrElemFT"/>
    <xs:enumeration value="FTImm"/>
    <xs:enumeration value="FlowsTo"/>
    <xs:enumeration value="InstFldFT"/>
    <xs:enumeration value="Label2Obj"/>
    <xs:enumeration value="Label2Prim"/>
    <xs:enumeration value="Label2PrimArrEl"/>
    <xs:enumeration value="Label2PrimFld"/>
    <xs:enumeration value="Label2Ref"/>
    <xs:enumeration value="LabelObjIn"/>
    <xs:enumeration value="Obj2Prim"/>
    <xs:enumeration value="Obj2Ref"/>
    <xs:enumeration value="Obj2Sink"/>
    <xs:enumeration value="Prim2Sink"/>
    <xs:enumeration value="Src2Sink"/>
    <xs:enumeration value="XferObjOut"/>
    <xs:enumeration value="T1"/>
    <xs:enumeration value="T2"/>
    <xs:enumeration value="T3"/>
    <xs:enumeration value="T4"/>
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="TemporarySymbol">
  <xs:restriction base="xs:token">
    <xs:pattern value="%[0-9]+"/>
  </xs:restriction>
</xs:simpleType>

<!-- BASE STEP TYPES =======================================================-->

<xs:complexType name="BaseStep" abstract="true">
  <xs:attribute name="reverse" type="xs:boolean" use="required"/>
  <xs:attribute name="from" type="NodeName" use="required"/>
  <xs:attribute name="to" type="NodeName" use="required"/>
</xs:complexType>

<xs:complexType name="MaybeIndexedStep" abstract="true">
  <xs:complexContent>
    <xs:extension base="BaseStep">
      <xs:attribute name="index" type="xs:nonNegativeInteger"/>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<!-- Head of the 'step' substitution group, serves as a placeholder for any
     kind of element that can appear as a sub-step of a non-terminal step. -->
<xs:element name="step" type="BaseStep" abstract="true"/>

<!-- TERMINAL STEP TYPES ===================================================-->

<xs:complexType name="TerminalStep" abstract="true">
  <xs:complexContent>
    <xs:extension base="BaseStep"/>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="IndexedTerminalStep" abstract="true">
  <xs:complexContent>
    <xs:extension base="TerminalStep">
      <xs:attribute name="index" type="xs:nonNegativeInteger" use="required"/>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<!-- More correctly, steps that stay within the same context. -->
<xs:complexType name="IntraProceduralStep">
  <xs:complexContent>
    <xs:extension base="TerminalStep"/>
  </xs:complexContent>
</xs:complexType>
<xs:element name="arrLoadPrim" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="arrStorePrim" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="arrStoreRef" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="asgnPrim" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="asgnRef" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="prim2Prim" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="prim2Ref" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="prim2SinkByPrim" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="prim2SinkByRef" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="ref2Prim" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="ref2Ref" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="ref2SinkByPrim" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="ref2SinkByRef" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="sinkLabel" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="srcLabel" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="statStorePrim" type="IntraProceduralStep" substitutionGroup="step"/>
<xs:element name="statStoreRef" type="IntraProceduralStep" substitutionGroup="step"/>

<xs:complexType name="CallStep">
  <xs:complexContent>
    <xs:extension base="IndexedTerminalStep"/>
  </xs:complexContent>
</xs:complexType>
<xs:element name="paramPrim" type="CallStep" substitutionGroup="step"/>
<xs:element name="paramRef" type="CallStep" substitutionGroup="step"/>

<xs:complexType name="ReturnStep">
  <xs:complexContent>
    <xs:extension base="IndexedTerminalStep"/>
  </xs:complexContent>
</xs:complexType>
<xs:element name="retPrim" type="ReturnStep" substitutionGroup="step"/>
<xs:element name="retRef" type="ReturnStep" substitutionGroup="step"/>

<!-- Steps (other than calls and returns) that cross to a new context. Still
     expected to retain a consistent view of the stack. -->
<xs:complexType name="CtxtCrossingStep">
  <xs:complexContent>
    <xs:extension base="IndexedTerminalStep"/>
  </xs:complexContent>
</xs:complexType>
<xs:element name="label2Prim" type="CtxtCrossingStep" substitutionGroup="step"/>
<xs:element name="label2Ref" type="CtxtCrossingStep" substitutionGroup="step"/>
<xs:element name="new" type="CtxtCrossingStep" substitutionGroup="step"/>

<!-- Steps that can arbitrarily set the context, even to otherwise invalid
     states. -->
<xs:complexType name="CtxtSettingStep">
  <xs:complexContent>
    <xs:extension base="IndexedTerminalStep"/>
  </xs:complexContent>
</xs:complexType>
<xs:element name="statLoadPrim" type="CtxtSettingStep" substitutionGroup="step"/>
<xs:element name="statLoadRef" type="CtxtSettingStep" substitutionGroup="step"/>

<xs:complexType name="StoreStep">
  <xs:complexContent>
    <xs:extension base="IndexedTerminalStep"/>
  </xs:complexContent>
</xs:complexType>
<xs:element name="storePrim" type="StoreStep" substitutionGroup="step"/>
<xs:element name="storeRef" type="StoreStep" substitutionGroup="step"/>

<xs:complexType name="LoadStep">
  <xs:complexContent>
    <xs:extension base="IndexedTerminalStep"/>
  </xs:complexContent>
</xs:complexType>
<xs:element name="loadPrim" type="LoadStep" substitutionGroup="step"/>
<xs:element name="loadRef" type="LoadStep" substitutionGroup="step"/>

<!-- NON-TERMINAL STEP TYPES ===============================================-->

<xs:complexType name="NonTerminalStep" abstract="true">
  <xs:complexContent>
    <xs:extension base="MaybeIndexedStep">
      <xs:sequence>
    <xs:element ref="step" minOccurs="0" maxOccurs="2">
      <xs:annotation>
            <xs:appinfo>
              <jxb:property name="subSteps"/>
            </xs:appinfo>
      </xs:annotation>
    </xs:element>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="GrammarNTStep">
  <xs:complexContent>
    <xs:extension base="NonTerminalStep">
      <xs:attribute name="symbol" type="GrammarNTSymbol" use="required"/>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
<xs:element name="NTStep" type="GrammarNTStep" substitutionGroup="step"/>

<xs:complexType name="TemporaryStep">
  <xs:complexContent>
    <xs:extension base="NonTerminalStep">
      <xs:attribute name="symbol" type="TemporarySymbol" use="required"/>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
<xs:element name="TempStep" type="TemporaryStep" substitutionGroup="step"/>

<!-- TOP-LEVEL ELEMENTS ====================================================-->

<xs:complexType name="Path">
  <xs:sequence>
    <xs:element ref="NTStep">
      <xs:annotation>
        <xs:appinfo>
          <jxb:property name="topStep"/>
        </xs:appinfo>
      </xs:annotation>
    </xs:element>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="Edge">
  <xs:sequence>
    <xs:element name="path" type="Path" minOccurs="1" maxOccurs="unbounded">
      <xs:annotation>
        <xs:appinfo>
          <jxb:property name="paths"/>
        </xs:appinfo>
      </xs:annotation>
    </xs:element>
  </xs:sequence>
  <xs:attribute name="from" type="NodeName" use="required"/>
  <xs:attribute name="to" type="NodeName" use="required"/>
</xs:complexType>

<xs:complexType name="PathsList">
  <xs:sequence>
    <xs:element name="edge" type="Edge" minOccurs="0" maxOccurs="unbounded">
      <xs:annotation>
        <xs:appinfo>
          <jxb:property name="edges"/>
        </xs:appinfo>
      </xs:annotation>
    </xs:element>
  </xs:sequence>
</xs:complexType>

<xs:element name="paths" type="PathsList"/>

</xs:schema>

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

它抱怨一个名为NTStep的元素,并且你发布的样本中没有这样的元素。因此,我会仔细检查一下,检查您正在处理的XML是您认为正在处理的XML。