简体   繁体   English

如何使用javascript解析xml键名及其值?

[英]How to parse xml key name and its value using javascript?

I am receiving an xml data like the example below from a remote api. 我正在从远程api接收类似下面的示例的xml数据。 Could anyone show me how I can get the key names and their corresponding key values and print them? 谁能告诉我如何获取密钥名称及其对应的密钥值并打印出来? For example I want to print all categoryIcon and category values. 例如,我要打印所有categoryIcon和category值。

javascript : javascript:

$.get("http://www.someapisite.com/test.php",
        {

          dataType: "jsonp"
        },

        function(data,status){

       //here i want to print the key names and its corresponding values
}

xml to parse: 要解析的xml:

<?xml version="1.0" encoding="UTF-8"?>
<dict><key>ItemLists</key>
<array><dict><key>id</key>
<string>1</string>
<key>name</key>
<string>fruits</string>
<key>category</key>
<string>US Fruits</string>
<key>categoryIcon</key>
<string>http://www.somsite.com/categories/1.jpg</string>
<key>country</key>
<string>US</string>
</dict>

<dict><key>id</key>
<string>2</string>
<key>name</key>
<string>Vegetable</string>
<key>category</key>
<string>Eu Vegetable</string>
<key>categoryIcon</key>
<string>http://www.somsite.com/categories/2.jpg</string>
<key>country</key>
<string>EU</string>
</dict>

</array>
</dict>

edit: 编辑:

For each set of dict i want to print a <tr> just like this: 对于每组字典,我都想像这样打印<tr>

 var div = "<tr id=\""+i+"\">\n" +
    "<td>"+i+"</td>\n" +
    "<td><img src=\""+ categoryIcon +"\" height=\"42\" width=\"42\"></td>\n" +
    "<td>\n" +
    "<a href=\"javascript:doit('id=" + id + "&name=" + name + "&category=" + category + "&categoryIcon=" + categoryIcon + "','"+ country +"')\" onclick=\"selectLink(this);\">" + name + "</a><br> \n" +
    "<br></td></tr>\n\n";

    $("#myDiv").append(div);

First you need to parses string into an XML document using parseXML() then you can find your key/value in document. 首先,您需要使用parseXML()将字符串解析为XML文档,然后才能在文档中找到键/值。 You should find key tag in array tag and iterate it using .each() . 您应该在array标记中找到key标记,并使用.each()对其进行迭代。 Also value of key is in next string tag that you can use .next() to select it. key值也位于下一个string标记中,您可以使用.next()来选择它。

var xml = $("#xml").html();
$(xml).find("array key").each(function(){
    var key = $(this).text();
    var value = $(this).next().text();  
    console.log(key + "=" + value);
});

 var xml = $("#xml").html(); $(xml).find("array key").each(function(){ console.log($(this).text() +"="+ $(this).next().text()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="xml"> <dict> <key>ItemLists</key> <array> <dict> <key>id</key> <string>1</string> <key>name</key> <string>fruits</string> <key>category</key> <string>US Fruits</string> <key>categoryIcon</key> <string>http://www.somsite.com/categories/1.jpg</string> <key>country</key> <string>US</string> </dict> <dict> <key>id</key> <string>2</string> <key>name</key> <string>Vegetable</string> <key>category</key> <string>Eu Vegetable</string> <key>categoryIcon</key> <string>http://www.somsite.com/categories/2.jpg</string> <key>country</key> <string>EU</string> </dict> </array> </dict> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM