2010-07-07 119 views
0

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

这是从Web服务返回的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的statusLocation属性:ExecuteResponse节点?

非常感谢。

+0

可能重复/ 3187819/jquery-parsing-xml) – 2010-07-07 09:57:06

+1

你已经问过这个问题,并且收到了正确答案。正如你指出的那样,你不需要解析XML:如果你使用'“xml”'作为dataType,你将在'success'回调中收到一个XML文档。 – 2010-07-07 09:57:34

+0

即使您坚持自己解析xml,无论如何,在另一个问题中已经给出了答案:'xmlDoc.documentElement.getAttribute(“statusLocation”)'。 – 2010-07-07 10:09:17

回答

1

你可以做到这一点比

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

这么简单得多,而不是使用ActiveX控件[\ [jQuery的\]解析XML(http://stackoverflow.com/questions的