我尝试解析以下类型的 XML(数据来自 getMembers.php):
I try parsing the following type of XML (data from getMembers.php):
<?xml version="1.0" encoding="ISO-8859-1"?>
<members>
<id>3422345</id>
<name>Bill Gates</name>
<id>232311</id>
<name>Bob Barker</name>
</members>
我使用下面的代码来解析它:
And I use the code below to parse it:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "getMembers.php",
cache: false,
dataType: "xml",
success: function(xml) {
$(xml).find('members').each(function(){
var name = $(this).find("name").text()
alert(name);
});
}
});
});
</script>
没有错误,但我同时得到了两个名字.
No error but I get both names at the same time.
喜欢:
比尔·盖茨鲍勃·巴克<--第一次循环>
Bill GatesBob Barker <--first loop>
代替
比尔盖茨<--第一个循环>
Bill Gates <--first loop>
Bob Barker <--第二个循环>
Bob Barker <--second loop>
任何帮助都会很棒!
我认为你迭代的是成员,而不是名字:
I think you iterate over members, not names:
success: function(xml) {
$(xml).find('members').each(function(){
$(this).find("name").each(function(){
var name = $(this).text();
alert(name);
});
});
}
或者您的 XML 应该如下所示:
Or maybe your XML should looks like:
<members>
<id>3422345</id>
<name>Bill Gates</name>
</members>
<members>
<id>232311</id>
<name>Bob Barker</name>
</members>
这篇关于AJAX/Jquery XML 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!