2017-12-04 5 views
0

jqueryフィルタ関数を使用してXMLを解析しようとしていますが、問題が発生しています。 私のXMLは次のようになります。XMLデータを取得する - Jquery - Filter

<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のは次のようになります。

$(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)は空のオブジェクトを返しました。なぜなら、データには別のデータタグが含まれていないからです(エラーが正しく理解されていれば)。

関連する問題