java xml验证soap-enc命名空间异常

时间:2015-01-16 15:23:14

标签: java web-services xml-validation

我需要针对xsd验证我的网络服务对象,并通过以下方式进行验证:

SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setResourceResolver(new ResourceResolver());
Source schemaFile = new StreamSource('Input stream with my xsd');
factory.newSchema(schemaFile);

最后一行 - factory.newSchema(schemaFile);使用使用soap-enc命名空间的xsd文件时抛出和异常。下面是xsd文件的一部分,命名空间声明和使用命名空间的复杂类型。

<xsd:schema
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/">
      <xsd:complexType name="name">
                <xsd:sequence>
                    <xsd:element name="id" type="xsd:string"/>
                    <xsd:element name="names" type="soap-enc:Array"/>
                </xsd:sequence>
            </xsd:complexType>

例外情况是:org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 62; src-resolve.4.2: Error resolving component 'soap-enc:Array'. It was detected that 'soap-enc:Array' is in namespace 'http://schemas.xmlsoap.org/soap/encoding/', but components from this namespace are not referenceable from schema document 'null'. If this is the incorrect namespace, perhaps the prefix of 'soap-enc:Array' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'null'.

1 个答案:

答案 0 :(得分:1)

此XML架构引用类型soap-enc:Array,该类型未在此架构文件中定义。所以你必须包含定义它的文件:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/">
  <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"
              schemaLocation="..."/>
  <!-- ...

您可以在Web上的定义命名空间的位置找到相应的XML架构。我认为最好下载它并从本地文件系统中使用它,如果你经常需要它通常会更有效。但是,将URL用作schemaLocation也应该可以工作。