如何访问SOAP响应属性?

8

所有

最近几天我一直在寻找如何使用JS访问SOAP,最终我从这个链接中找到了解决方案Simplest SOAP example

现在我能够在警报中获取我的SOAP请求。 但是我想使用它的属性并想要打印响应(即解析响应并显示)

这是我的代码

const 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
    let json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);
    const 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');
const 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等,我该怎么做?
提前感谢。
2个回答

6
尝试使用这种方式。
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
自1.5版本以来,jQuery添加了一个parseXML实用程序,用于将字符串解析为XML,并相应地访问您的节点。
在您的示例中,您可以按以下方式使用它,以使response成为服务器的响应:
  var xmlDoc = $.parseXML( response ),
      $xml = $( xmlDoc ),
      $value= $xml.find( "errorShortDesc" );
      //do what you want with the value
  console.log($value.text());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接