2010-07-08 291 views
2

的第一個孩子我有這樣的XML文件。 我會提取Status節點的第一個子節點並提醒它。 我該怎麼做?獲取XML節點

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <ns3:ExecuteResponse xmlns:ns1="http://www.opengis.net/ows/1.1" xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns3="http://www.opengis.net/wps/1.0.0" statusLocation="http://r/6de0e29d-8d67-40c3-8189-03f7cd23a0cb" serviceInstance="http://-service/services/http-post" version="1.0.0" service="WPS"> 
    <ns3:Process ns3:processVersion="0.2"> 
     <ns1:Identifier>123</ns1:Identifier> 
     <ns1:Title xml:lang="en-US">Bioclim</ns1:Title> 
     <ns1:Abstract xml:lang="en-US">Uses mean and standard deviation for each environmental variable separately to calculate bioclimatic envelopes. Level of fitness between the environmental values on a point and the respective envelopes classifies points as Suitable, Marginal, or Unsuitable for presence. 
Implements the Bioclimatic Envelope Algorithm. For each given environmental variable the algorithm finds the mean and standard deviation (assuming normal distribution) associated to the occurrence points. Each variable has its own envelope represented by the interval [m - c*s, m + c*s], where 'm' is the mean; 'c' is the cutoff input parameter; and 's' is the standard deviation. Besides the envelope, each environmental variable has additional upper and lower limits taken from the maximum and minimum values related to the set of occurrence points. 
In this model, any point can be classified as: 
Suitable: if all associated environmental values fall within the calculated envelopes; 
Marginal: if one or more associated environmental value falls outside the calculated envelope, but still within the upper and lower limits. 
Unsuitable: if one or more associated enviromental value falls outside the upper and lower limits. 
Bioclim's categorical output is mapped to probabilities of 1.0, 0.5 and 0.0 respectively.</ns1:Abstract> 
    </ns3:Process> 
    <ns3:Status creationTime="2010-07-08T16:33:03.326+02:00"> 

     <ns3:ProcessStarted/> 
    </ns3:Status> 
    <ns3:ProcessOutputs> 
     <ns3:Output> 
      <ns1:Identifier>servicesDocuments</ns1:Identifier> 
      <ns1:Title xml:lang="en-US">A json document in which can be published some XML documents relative to the run</ns1:Title> 
      <ns3:Data> 
       <ns3:LiteralData>http://hermes.pin.unifi.it:9090/wps-1.0.0-service/publisher/953baf3a-dddf-49cc-a673-7491d82f80b7</ns3:LiteralData> 

      </ns3:Data> 
     </ns3:Output> 
     <ns3:Output> 
      <ns1:Identifier>extendedStatus</ns1:Identifier> 
      <ns1:Title xml:lang="en-US">WPS status in JSON format</ns1:Title> 
      <ns3:Data> 
       <ns3:LiteralData>{&quot;globalStatus&quot;:&quot;processExecution&quot;,&quot;resourceList&quot;:[]}</ns3:LiteralData> 

      </ns3:Data> 
     </ns3:Output> 
    </ns3:ProcessOutputs> 
</ns3:ExecuteResponse> 

我正在使用jQuery。 我已經轉換與DOM中的XML,現在我必須查詢其用於提取狀態的子節點。

$.ajax({ 
    type: 'GET', 
    url: "php/proxy.php?proxy_url=" + obj.requests[i].output, 
         contentType: "text/xml", 
    dataType: "text/xml", 
    async:false, 
    success: function(xml){ 
     var $dom = $.xmlDOM(xml, function(error){ 
    alert('A parse error occurred! ' + error); 
    }); 
           //extract data 

     }}); 

非常感謝。

回答

3

這對我有用。我優化了你如何處理你從中獲取xml的url。

var url = 'php/proxy.php'; 
var obj = { requests: [{output: 'foo'}] }, i = 0; // shims 
$.ajax({ 
    url: url, 
    type: 'GET', 
    // This is a better way to pass querystring params 
    data: { 'proxy_url': obj.requests[i].output }, 
    dataType: "xml", 
    async: false, 
    success: function(xmlData, textStatus, XMLHttpRequest){ 
     // How do we get ahold of the ns3:Status element? 
     // We must escale the ":" character: 
     var $ns3Status = $('ns3\\:Status', xmlData); 
     alert($ns3Status.attr('creationTime')); // we can get an attribute 
     alert($ns3Status.get(0).tagName); // we can get tagName 

     // Let's iterate over the children of the ns3:Status element 
     $ns3Status.children().each(function(){ 
      alert($(this).get(0).tagName); 
     }); 
    } 
}); 
1

使用「XML」數據類型,以避免解析XML。 (這是我第三次向你提出這個建議,你從未說過爲什麼你不能這麼做)。請注意,在IE瀏覽器的第一個孩子將是一個<ns3:ProcessStarted>元素,而在其他瀏覽器將是一個包含文本節點空白。

$.ajax({ 
    type: 'GET', 
    url: "php/proxy.php?proxy_url=" + obj.requests[i].output, 
    contentType: "text/xml", 
    dataType: "xml", 
    async: false, 
    success: function(xml) { 
     var statusNode = xml.getElementsByTagName("ns3:Status")[0]; 
     alert(statusNode.firstChild); 
    } 
}); 

你可以換的XML文檔中的jQuery對象,如果你喜歡,那將得到全面的空白點的問題,忽略它:

$.ajax({ 
    type: 'GET', 
    url: "php/proxy.php?proxy_url=" + obj.requests[i].output, 
    contentType: "text/xml", 
    dataType: "xml", 
    async: false, 
    success: function(xml) { 
     var node = $(xml).find("ns3\\:Status").find(":first-child"); 
     alert(node[0].tagName); 
    } 
}); 
0

有一些相當不錯的jQuery插件這可能有助於這一點。我最近使用了一個xml2json插件,所以我可以使用JSON語法引用返回的XML的特定部分。

var ExecuteResponse = jQuery.xml2json(xml); 
var firstStatusChild = ExecuteResponse.Status[0]; 
+1

這是完全不必要的。使用「xml」作爲'dataType',你得到一個XML文檔。 – 2010-07-08 15:29:31

+0

我同意,有助於瞭解xml2json,但對於這項任務來說過分了。 – artlung 2010-07-08 16:55:23