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

java xml validation soap-enc namespace exception

我需要根据 xsd 验证我的 Web 服务对象,我正在这样做:

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'.

此 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 应该也可以。