我正在尝试针对 WSDL 验证 SOAP 响应消息,并且我正在遵循此问题中给出的示例。但是,我得到了下面的例外。
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'SOAP-ENC:Array' to a(n) 'type definition' component.
我看到了一个关于类似错误的问题,但我不确定这是否是同一个问题。我也不明白为什么当它出现在SOAP 规范SOAP-ENC:Array
中时在该问题中被认为是非标准的。
这是我的验证码。异常就Schema schema = schemaFactory.newSchema(schemas)
行了。
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document wsdlDoc = db.newDocument();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source wsdlSource = new StreamSource(new File("d:\\temp\\demo.wsdl"));
transformer.transform(wsdlSource, new DOMResult(wsdlDoc));
NodeList schemaNodes = wsdlDoc.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema");
int nrSchemas = schemaNodes.getLength();
Source[] schemas = new Source[nrSchemas];
for (int i = 0; i < nrSchemas; ++i)
{
schemas[i] = new DOMSource(schemaNodes.item(i));
}
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemas);
Validator validator = schema.newValidator();
Source soapMessage = new StreamSource(new File("d:\\temp\\soapmessage.xml"));
Result result = new StreamResult(System.out);
validator.validate(soapMessage, result);
我已经修剪了 WSDL,只留下了相关部分,或者至少我认为相关的部分。如果需要更多,我会更新问题。
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="run:demo"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="run:demo">
<types>
<xsd:schema targetNamespace="run:demo">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/"/>
<xsd:complexType name="itemsCT">
<xsd:all>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="Address" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
<xsd:complexType name="itemsArray">
<xsd:complexContent>
<xsd:restriction base="SOAP-ENC:Array">
<xsd:attribute ref="SOAP-ENC:arrayType"
wsdl:arrayType="tns:itemsCT[]"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
</types>
</definitions>