2010-07-08 2 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를 사용하고 있습니다. xml을 dom으로 변환 한 후 Status 노드의 자식을 추출하기 위해 쿼리해야합니다.

$.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"dataType을 사용하여 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); 
    } 
}); 

만약 당신이 좋아하면 당신은 jQuery를 객체의 XML 문서를 포장 수, 그리고 그것을 무시 공백 노드의 문제를 피해 갈 것이다

$.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

완전히 필요하지 않습니다. 'dataType'으로 "xml"을 사용하면 XML 문서를 다시 얻을 수 있습니다. –

+0

xml2json에 대해 알아두면 유용 하겠지만,이 작업에는 과도 함이 있습니다. – artlung

관련 문제