2010-07-07 120 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的