XML sax解析异常。内容错误无效

时间:2015-12-17 17:19:08

标签: java xml web-services xml-parsing

这是我的Xml

  <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:ch="http://abc.def.com/admin/find/types/v1" 
           targetNamespace="http://abc.def.com/admin/find/types/v1"
         elementFormDefault="qualified" >

         <import namespace="http://esi.osm.com/commonheader/v3" schemaLocation="CommonHeaderV3_3.xsd" />
    ...  
        <xsd:complexType name="SystemConfig">
            <xsd:sequence>
                <element name="System"      type="xsd:int"        minOccurs="1" />
                <element name="NetworkType" type="ch:NetworkType" minOccurs="1" />
                <element name="Location"    type="xsd:string"     minOccurs="0" />
                <element name="Address"     type="xsd:string"     minOccurs="0" />
                <element name="City"        type="xsd:string"     minOccurs="0" />
            </xsd:sequence>
          </xsd:complexType>

       <simpleType name="NetworkType" >
            <restriction base="string">
                <enumeration value="CABLE" />
                <enumeration value="DSL" />
            </restriction>
       </simpleType>

我试图运行我的网络服务时遇到的错误就是这个..

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'ns3:Location'. One of '{"http://abc.def.com/admin/find/types/v1":NetworkType}' is expected.
    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)

编辑: Java代码

    NetworkType networkType= null;
        if(db.getNetworkType().equals("cable")) {
            networkType= NetworkType.fromValue("CABLE");
        } 
        if (db.getNetworkType().equals("dsl")) {
            networkType = NetworkType.fromValue("DSL");
        } 
   ntList.setNetworkType(networkType);
   log.info(networkType); //prints null....

为networkType打印的值为null,但实际上我能够获取db值 实际上我发现了问题....数据库中包含空格,因此传递的值并不完全正确。 添加trim()后它运行良好

1 个答案:

答案 0 :(得分:1)

在没有看到XML的情况下,仅从错误消息中可以清楚地看到解析器遇到了ns3:Location元素,意识到找不到前面的必需的 ch:NetworkType元素,并抱怨遗漏。我们可以说ch:NetworkType是必需的,因为您的XSD片段在minOccurs="1"的内容模型中ch:NetworkType上有SystemConfig