【问题标题】:Getting key value pairs out of xml file with query使用查询从 xml 文件中获取键值对
【发布时间】:2014-05-03 11:42:51
【问题描述】:

我将我的 iTunes 库导出到一个 xml 文件,它看起来像这样:

<key>725</key>
    <dict>
        <key>Track ID</key><integer>725</integer>
        <key>Name</key><string>Sandstorm</string>
        <key>Artist</key><string>Daro</string>
        <key>Album</key><string>Gunther D</string>
        <key>Genre</key><string>Dance</string>
        <key>Kind</key><string>MPEG-audiobestand</string>
        <key>Size</key><integer>6937997</integer>
        <key>Total Time</key><integer>223451</integer>
        <key>Track Number</key><integer>3</integer>
        <key>Year</key><integer>2013</integer>
        <key>Library Folder Count</key><integer>1</integer>
    </dict>

我进行了 ajax 调用来读出 xml 文件。但是现在我想获取专辑密钥和字符串,但我不知道如何获取它。

    $(document).ready(function(){
$.ajax({
url: "Bibliotheek.xml",
dataType: "xml",
success: function(data){
    var $xml = $(data);
    var $key = $xml.find('key').each(function(){
        console.log($key);

        });
    }    
});

});

现在它在我的控制台中记录了我的所有密钥,但我只想要专辑密钥

【问题讨论】:

标签: jquery ajax xml


【解决方案1】:

您可以过滤键

$(document).ready(function(){
    $.ajax({
        url      : "Bibliotheek.xml",
        dataType : "xml",
        success  : function(data){

            var album = $(data).find('key').filter(function() {
                return $(this).text().indexOf('Album') != -1;
            }).next('string').text();

        }    
    });
});

【讨论】:

  • 最后一个问题,如果你能帮助我,那就太好了。我想将它们全部对齐在彼此下方,但是如果我添加一个
    标签,他会继续将它们彼此对齐
  • @adeneo 如果我们有很多组 并且希望每个 dict 设置键值(字符串值)都打印在 中,我们如何实现呢?谢谢
猜你喜欢
  • 2022-01-04
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
相关资源
最近更新 更多