我刚问了一个非常类似的问题,但是和Json在一起,现在我遇到了xml问题。
XML:
<dinosaurs>
<dinosaur>
<name>Staurikosaurus</name>
<group>Saurischia</group>
<diet>Carnivore</diet>
<period>Triassic</period>
</dinosaur>
<dinosaur>
<name>Diplodocus</name>
<group>Saurischia</group>
<diet>Herbivore</diet>
<period>Jurassic</period>
</dinosaur>
<dinosaur>
<name>Stegosaurus</name>
<group>Ornithischia</group>
<diet>Herbivore</diet>
<period>Jurassic</period>
</dinosaur>
<dinosaur>
<name>Tyrannosaurus</name>
<group>Saurischia</group>
<diet>Carnivore</diet>
<period>Cretaceous</period>
</dinosaur>
</dinosaurs>
我想将名字放入未排序的列表
我的代码:
var inner = '';
$(xml).find('dinosaurs').each(function(){
$(this).find('dinosaur').each(function(){
$(this).find("name").each(function(){
var name = $(this).text();
inner += "<li>"+name+"</li>";
}
}
}
这绝对没有输出,有人能告诉我哪里出错了吗? 拜托,谢谢
答案 0 :(得分:0)
试试这个:
var xml = "<dinosaurs> <dinosaur> <name>Staurikosaurus</name> <group>Saurischia</group> <diet>Carnivore</diet> <period>Triassic</period> </dinosaur> <dinosaur> <name>Diplodocus</name> <group>Saurischia</group> <diet>Herbivore</diet> <period>Jurassic</period> </dinosaur> <dinosaur> <name>Stegosaurus</name> <group>Ornithischia</group> <diet>Herbivore</diet> <period>Jurassic</period> </dinosaur> <dinosaur> <name>Tyrannosaurus</name> <group>Saurischia</group> <diet>Carnivore</diet> <period>Cretaceous</period> </dinosaur> </dinosaurs>";
var xmlDoc = $.parseXML(xml);
var inner = '';
$(xmlDoc).find('dinosaurs').each(function ()
{
$(this).find('dinosaur').each(function ()
{
$(this).find("name").each(function ()
{
var name = $(this).text();
inner += "<li>" + name + "</li>";
});
});
});