2013-07-16 36 views
1

我想驗證WSDL的SOAP響應消息,並且我遵循給定的示例in this question。但是,我得到下面的例外。SAXParseException無法將SOAP-ENC:Array解析爲類型定義組件

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'SOAP-ENC:Array' to a(n) 'type definition' component.

我見過a question有關類似錯誤,但我不知道這是否是同樣的問題。我也不明白爲什麼SOAP-ENC:Array在出現在SOAP spec中時被認爲是非標準問題。

這是我的驗證碼。 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> 

回答

1

眼前的問題是,模式驗證被稱爲不加載任何架構文檔命名空間http://schemas.xmlsoap.org/soap/encoding/ - 可能是因爲它是一個通用的驗證,沒有SOAP命名空間的任何內在的知識,或者是因爲它沒有沒有設法從schemas.xmlsoap.org的服務器檢索模式文檔。

如果您有http://schemas.xmlsoap.org/soap/encoding/http://schemas.xmlsoap.org/wsdl/名稱空間的模式的本地副本,您可以嘗試向模式中的兩個xsd:import元素添加模式位置信息。如果你沒有本地副本,那麼我希望你能夠比我剛剛做的更好地獲得schemas.xmlsoap.org的迴應。

相關問題