我正在尝试使用jquery解析我的xml以创建表。我有下面的代码。我试图让它在标签中循环,但是循环不起作用,我认为循环中的任何东西都没有运行。我有80%的把握我的xml是正确的,因为当查找任何标记时,如果没有.each(),它将输出所有但不是分开的。有人对我做错了什么有什么建议吗?
$(document).ready(function(){
$("button").click(function(){
$.get("xml.xml", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
xmlDoc = $.parseXML( data ),
$xml = $( xmlDoc ),
var data2 = $xml.find("name").text());
jQuery(xml).find("films").each(function()
{
movieId = jQuery(this).find("id").text();
alert(movieId);
});这是我的xml文件。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:films xmlns:ns2="xml.jaxb.model">
<filmList>
<films>
<id>11003</id>
<name>THE ADVENTURES OF PRISCILLA, QUEEN OF THE DESERT</name>
<year>1994</year>
<director>STEPHEN ELLIOTT</director>
<stars>TERENCE STAMP, GUY PEARCE</stars>
<review>Whimsical and warm-hearted tale of three Australian drag queens as they drive through the Australian outback on their way to the city. Stopping in small towns to refuel Priscilla, their bus, they start to give performances to the sometimes amazed, sometimes entranced townsfolk. Stamp, Hugo Weaving and Guy Pearce (L.A.Confidential) are terrific in their roles.</review>
</films>
<films>
<id>11099</id>
<name>THE Lion QUEEN</name>
<year>1993</year>
<director>ROGER ALLERS & ROB MINKOFF</director>
<stars>ANIMATED</stars>
<review>The most successful Disney film ever, and rightly so. A superbly animated tale with stirring visuals and good songs. A young lion returns to the pride to claim his title as The Lion King after discovering his father was killed by his evil uncle. Score by Sir Tim Rice and Elton John, with the Oscar winning Can You Feel The Love Tonight.</review>
</films>
</filmList>
</ns2:films>发布于 2021-01-14 07:52:21
正如Barmar的评论所说,您的代码将不会运行,并且看起来您可能复制粘贴了错误的内容。但是,这些内容应该可以满足您的需求。
代码本身很有说明性,尽管您可以随时询问有关它的问题。
var xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:films xmlns:ns2="xml.jaxb.model"><filmList><films><id>11003</id><name>THE ADVENTURES OF PRISCILLA, QUEEN OF THE DESERT</name><year>1994</year><director>STEPHEN ELLIOTT</director><stars>TERENCE STAMP, GUY PEARCE</stars><review>Whimsical and warm-hearted tale of three Australian drag queens as they drive through the Australian outback on their way to the city. Stopping in small towns to refuel Priscilla, their bus, they start to give performances to the sometimes amazed, sometimes entranced townsfolk. Stamp, Hugo Weaving and Guy Pearce (L.A.Confidential) are terrific in their roles.</review></films><films><id>11099</id><name>THE Lion QUEEN</name><year>1993</year><director>ROGER ALLERS & ROB MINKOFF</director><stars>ANIMATED</stars><review>The most successful Disney film ever, and rightly so. A superbly animated tale with stirring visuals and good songs. A young lion returns to the pride to claim his title as The Lion King after discovering his father was killed by his evil uncle. Score by Sir Tim Rice and Elton John, with the Oscar winning Can You Feel The Love Tonight.</review></films></filmList></ns2:films>'
var parsedDoc = $.parseXML(xml)
var parsed = $(parsedDoc)
parsed.find("films").each(function(i, item)
{
console.log("Movie Id:" + $(item).find("id").text())
})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
https://stackoverflow.com/questions/65711333
复制相似问题