2011-08-01 52 views
7

所有我如何訪問SOAP響應屬性?

表格最近幾天我發現瞭如何使用JS訪問肥皂,畢竟我得到從這個鏈接Simplest SOAP example

的解決方案現在我能夠得到我的警報SOAP請求。 但我想使用它的屬性和要打印的響應(我的意思是解析響應和顯示)

這是我的代碼

var xmlhttp = new XMLHttpRequest(); 
        xmlhttp.open("POST", "http://service.project-development-site.de/soap.php",true); 
        xmlhttp.onreadystatechange=function() { 
        if (xmlhttp.readyState == 4) { 

        alert(xmlhttp.responseText); 

         // http://www.terracoder.com convert XML to JSON 
         var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML); 
         var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text; 
         // Result text is escaped XML string, convert string to XML object then convert to JSON object 
         json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result)); 
         alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text); 
        } 
        } 
        xmlhttp.setRequestHeader("SOAPAction", "http://service.project-development-site.de/soap.php"); 
        xmlhttp.setRequestHeader("Content-Type", "text/xml"); 
        var xml = '<?xml version="1.0" encoding="utf-8"?>' + 
        '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' + 
         '<soapenv:Header/>'+ 
          '<soapenv:Body>'+ 
           '<tem:loginUserSoapInPart>'+ 
           '<tem:userName>user</tem:userName>'+ 
           '<tem:passWord>pwd</tem:passWord>'+ 
           '<tem:accesToken>acktoken</tem:accesToken>'+ 
           '</tem:loginUserSoapInPart>'+ 
          '</soapenv:Body>'+ 
         '</soapenv:Envelope>'; 
        xmlhttp.send(xml); 

和我在警報得到響應這樣

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"> 
    <SOAP-ENV:Body> 
     <ns1:loginUserSoapOutPart> 
     <ns1:error> 
      <ns1:errorCode>0</ns1:errorCode> 
      <ns1:errorShortDesc>OK</ns1:errorShortDesc> 
      <ns1:errorLongDesc>SOAP request executed successfully .</ns1:errorLongDesc> 
     </ns1:error> 
     <ns1:soapOut> 
      <ns1:accesToken>accesToken</ns1:accesToken> 
      <ns1:ACK>ACK</ns1:ACK> 
     </ns1:soapOut> 
     </ns1:loginUserSoapOutPart> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

而我想顯示這個響應屬性,如errorShortDesc,errorLongDesc等... 我怎麼能?

由於提前

回答

5

嘗試用這種方式

var xmlResponse =xmlhttp.responseXML.documentElement; 

var fullNodeList = xmlResponse.getElementsByTagName("loginUserSoapOutPart"); 

for (var i=0; i < fullNodeList.length; i++) 
{ 
    var eachnode = new Option(); 
    eachnode.text = fullNodeList[i].childNodes[0].nodeValue; 
    eachnode.value = fullNodeList[i].attributes[0].value; 
} 
1

jQuery的,因爲1.5的parseXML工具添加到字符串解析爲XML,然後相應地

訪問節點在你的榜樣,您可以按如下方式使用它:response是服務器的響應:

var xmlDoc = $.parseXML(response), 
     $xml = $(xmlDoc), 
     $value= $xml.find("errorShortDesc"); 
     //do what you want with the value 
    console.log($value.text());