【问题标题】:Jquery parsing xml from requestJquery从请求中解析xml
【发布时间】:2010-07-07 09:34:32
【问题描述】:

我正在使用 Jquery。 我有一个从 web 服务返回的 xml。 所以我想从中提取一些价值。

这是一个从 webservice 返回的 xml 示例:

<ns3:ExecuteResponse xmlns:ns3="http://www.opengis.net/wps/1.0.0"
  xmlns:ns1="http://www.opengis.net/ows/1.1"
  xmlns:ns2="http://www.w3.org/1999/xlink"
  statusLocation="http://webservice:9090/b57c-43b8-40b2-8e09-4d6489df55be"
  serviceInstance="http://xyz.it:9090/services/http-post"
  version="1.0.0" service="XXX">
  <ns3:Process ns3:processVersion="0.2">
    <ns1:Identifier>OM_BIOCLIM</ns1:Identifier>
    <ns1:Title xml:lang="en-US">Bioclim</ns1:Title>
    <ns1:Abstract xml:lang="en-US">abcdefghi</ns1:Abstract>
  </ns3:Process>
  <ns3:Status creationTime="2010-07-07T10:42:05.768+02:00">
    <ns3:ProcessAccepted>ProcessConfiguration has been
    accepted.</ns3:ProcessAccepted>
  </ns3:Status>
  <ns3:ProcessOutputs />
</ns3:ExecuteResponse>

这是我的代码:

    $.ajax({
      type: 'POST',
      url: "php/geoproxy.php?url=http://www.abc.it:9090/1.0.0-service/services/http-post",//wpsUrl,
          contentType: "text/xml",
      data: xmlRequest,
      dataType: "text/xml",
      success: function(xml){

        var xmlDoc;                             
        if (window.DOMParser)
          {
          parser=new DOMParser();
          xmlDoc=parser.parseFromString(xml,"text/xml");
          }
        else // Internet Explorer
          {
          xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.async="false";
          xmlDoc.loadXML(xml);
          } 

            //extract statusLocation attribute
        },
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
      alert("error "+XMLHttpRequest); 
    } 
});

如何提取 ns3:ExecuteResponse 节点的 statusLocation 属性?

非常感谢。

【问题讨论】:

  • [jQuery] Parsing xml的可能重复
  • 您已经问过这个问题,并且得到了正确的答案。正如向您指出的那样,您不需要解析 XML:如果您使用 "xml" 作为数据类型,您将在 success 回调中收到一个 XML 文档。
  • 即使你坚持自己解析xml,反正在另一个问题中已经给你答案了:xmlDoc.documentElement.getAttribute("statusLocation")

标签: javascript jquery xml parsing


【解决方案1】:

你可以做的比这简单得多

 success: function(xml){
       statusLocation = $(xml).attr('statusLocation')
 }

不使用 ActiveX 控件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2023-03-18
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2019-08-10
    • 2017-06-01
    相关资源
    最近更新 更多