JAXB验证错误 - cvc-complex-type.2.4.a:从元素'codeSystem'开始发现无效内容。预计会有一个'{codeSystem}'

时间:2013-05-31 17:23:40

标签: java xml parsing validation jaxb

我正在尝试解析和验证XML(jaxb-impl-2.2.4.jar),我收到了错误:

  

cvc-complex-type.2.4.a:找到无效的内容   元素'codeSystem'。预计会有一个'{codeSystem}'。

我不确定是什么导致它,因为我认为我的XML看起来是正确的。

codeSystem的架构要求:

<xs:complexType name="GenericPropertyType">
     <xs:element name="codeSystem" type="tns:CodeSystem">
     </xs:element>
     <xs:element name="code" type="tns:Code">
     </xs:element>
     <xs:element name="codeText" type="tns:CodeText" minOccurs="0">
     </xs:element>
</xs:complexType>

codeSystem所属的GenericProperty Java类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GenericPropertyType", propOrder = {
    "codeSystem",
    "code",
    "codeText"
})
public class GenericPropertyType {

    @XmlElement(required = true)
    protected String codeSystem;
    @XmlElement(required = true)
    protected String code;
    @XmlElement
    protected String codeText;

    /**
     * Getters and Setters ommitted.
     * 
     */
}

正在解析的XML:

<genericProperty>
    <codeSystem>8B-30-33</codeSystem>
    <code>EMAIL_RETRY_COUNT</code>
    <codeText>5</codeText>
</genericProperty>

我在genericPropertycodeSystem元素(如xmlns="http://www.somedomain.com/context"中提供和不提供命名空间的情况下尝试过,但错误仍然相同。有什么想法吗?

修改 的 架构中的CodeSystem类型:

<xs:simpleType name="CodeSystem">
<xs:annotation>
  <xs:documentation>Simple Type with Input Restictions</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
  <xs:whiteSpace value="collapse" fixed="true"/>
  <xs:maxLength value="64" fixed="true"/>
</xs:restriction>

1 个答案:

答案 0 :(得分:5)

我能够通过将此行添加到正在验证的XmlSchema(XSD文件)来解决此问题:elementFormDefault =“qualified”

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.somedomain.com/context"
        xmlns:tns="http://www.somedomain.com/context"
        **elementFormDefault="qualified"**>

我在我的package-info.java类中声明了这个,但是我想知道它是否在提供给JAXB解析器的XSD架构中。