我没有解析xml文档的经验,所以请耐心等待。在过去的几天里,我正在努力解析下面给出的xml示例!
在搜索互联网并阅读其他评论后,我得出结论,最好的办法是使用$ parseXML作为我处理的xml类型。
我验证了xml并使用了以下代码,但我一直得到无效的xml! 我的目标是获取属于一个<dict>
的所有字符串值,并在<tr>
中使用它们。所以,如果专家帮我解决这个破碎的代码,我感激不尽。
这是我想将键值放在div中的方式:
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);
javascript:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
function myfunction(){
alert("me here");
var 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>';
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc );
$($xml).each(function(){
//alert($(this).find("dict>string").text());
//alert($(this).find("dict>key").text());
alert($(this).find("dict>categoryIcon>string").text());
});
};
</script>
</head>
<body>
<button onclick="myfunction()">parse</button>
</body>
</html>
解析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>
答案 0 :(得分:0)
<script>
function myfunction() {
alert("me here");
var 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>';
xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc);
$.each($xml.find('dict string'), function () {
console.info($(this).text());
});
}
</script>
或者您也可以在each
声明$xml.find('dict>array dict string')