2014-03-13 45 views
1

我有一個XML文件,我想從該文件中查找一些值我怎麼可以這樣使用jquery搜索標籤值使用jQuery

<?xml version="1.0" encoding="UTF-8"?> 
<plist version="1.0"> 
    <dict> 
    <key>ID</key> 
    <string>B0A6EF3C-221F-4918-89C2-340B05F6A7AD</string> 
    <key>Name</key> 
    <string>name</string> 
    <key>Items</key> 
    <array> 
     <dict> 
     <key>Mode</key> 
     <integer>1000</integer> 
     <key>background</key> 
     <string>RGBA:0.000000,1.000000,1.000000,1.000000</string> 
     <key>Enabled</key> 
     <true/> 
     </dict> 
     <dict> 
     <key>Mode</key> 
     <integer>1000</integer> 
     <key>background</key> 
     <string>RGBA:0.000000,1.000000,1.000000,1.000000</string> 
     <key>Enabled</key> 
     <true/> 
     </dict> 
    </array> 
    </dict> 
</plist> 

我該怎麼如果我想搜索的值,例如獲得第二<dict>項目背景,怎麼樣使值,在這種情況下 真的,我想這樣做jQuery的

這是我嘗試到目前爲止

$.post("demo.xml",{},function(xml){ 
// Run the function for each dict tag inside array tag in the XML file 
    $('array',xml).each(function(i) { 
      $(this).find('dict').each(function(){ 

       $(this).find('key').each(function(){ 

          key = $(this).text(); 
          value = $(this).next().text(); 

          alert("key = " + key + " value = "+ value); 
        }); 
      }); 

    }); 

上面的jQuery代碼的工作很適合我,但每次返回鍵和它的價值。我想搜索特定值超出它例如

var value = $(this).find("background").value; 

我想的名字,如「背景」

+1

顯示您嘗試過的任何信息 – Hiral

+0

您可以參考此鏈接; http://stackoverflow.com/questions/19220873/how-to-read-xml-file-contents-in-jquery-and-display-in-html-elements –

+0

與哪些標籤? – Shin

回答

1
搜索

試着這麼做,

$(document).ready(function(){ 
$.ajax({ 
type: "GET" , 
url: "file.xml" , 
dataType: "xml" , 
success: function(xml) { 
    xmlDoc = $.parseXML(xml), 
    $xml = $(xmlDoc), 
    $title = $xml.find("dict").each(function(){ 
      var data=$(this); //here we will get the data 
     }); 
    } 
}); 
}); 
+0

問題是不讀取XML文件。我只是想通過它的關鍵名稱搜索值 – Anni

0

$(文件)。就緒(函數(){$ 獲得( 'myData.xml',函數(d){

 $(d).find('array').each(function() { 

     var $array = $(this); 

     var dict = $array.find('dict').each(function() { 
      var key = dict.find('key').each(function() { 
       var value = $(this) 
      }) 

     }) 
    }) 
}) 

});

+0

你認爲我得到的價值。這與我的代碼相同,但沒有工作,我需要通過它的關鍵名稱值是裏面字典標籤。並感謝您的重播Ashish Yadav – Anni