2014-03-13 48 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