2017-12-04 3 views
0

jquery 필터 함수를 사용하려고하는데 XML을 파싱했지만 문제가 있습니다.XML 데이터 가져 오기 - Jquery - 필터

<root> 
    <data name="0121C395-AFCE-49C8-A163-55E24325D691" xml:space="preserve"> 
     <value>Report an incident</value> 
    </data> 
    <data name="0121C395-AFCE-49C8-A163-55E24325D691_descr" xml:space="preserve"> 
     <value>In this service it's possible to report incidents.</value> 
    </data> 
    <data name="0121C395-AFCE-49C8-A163-55E24325D691_short" xml:space="preserve"> 
     <value>Report Incident</value> 
    </data> 
</root> 

을 그리고 내 JQuery와는 다음과 같습니다 : 내 XML은 다음과 같습니다

$(document).ready(function(){ 
    $.ajax({ 
     type: "GET", 
     url: "http://localhost/servicemarket/ReachOut/Services.xml", 
     dataType: "xml", 
     success: function(xml){ 
      var dataVal = $(xml).find('data').filter(function(){ 
       return $('data', this).attr('name') == '0121C395-AFCE-49C8-A163-55E24325D691'; 
      }); 
      dataVal.each(function(index, data){ 
       console.log($(data).find('value').text()); 
      }); 
     }, 
     error: function() { 
      alert("An error occurred while processing XML file."); 
     } 
    }); 
}); 

내 목표는 들어오는 데이터의 속성에 따라 노드 값을 얻는 것입니다. 모든 도움을 많이 받으실 수 있습니다! :) 감사합니다!

답변

0

귀하의 코드를 약간 수정해야합니다

$(document).ready(function(){ 
    $.ajax({ 
     type: "GET", 
     url: "http://localhost/servicemarket/ReachOut/Services.xml" 
     dataType: "xml", 
     success: function(xml){ 
      var dataVal = $(xml).find('data').filter(function(){ 

       return $(this).attr('name') == '0121C395-AFCE-49C8-A163-55E24325D691'; 
      }); 
      //console.log(dataVal); 
      dataVal.each(function(index, data){ 
       console.log($(data).find('value').text()); 
      }); 
     }, 
     error: function() { 
      alert("An error occurred while processing XML file."); 
     } 
    }); 
}); 

그래서, 객체 (발견()에 의해 발견) 현재 얻을 수 $(this)를 사용합니다. $('data', this)은 데이터에 다른 데이터 태그가 포함되어 있지 않기 때문에 (내가 올바르게 오류를 이해하는 경우) 빈 개체를 반환했습니다.

관련 문제