jQuery parseXML, .each()

Posted

技术标签:

【中文标题】jQuery parseXML, .each()【英文标题】: 【发布时间】:2021-04-19 00:30:06 【问题描述】:

我正在尝试使用 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 &amp; 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 data2 = $xml.find("name").text()); 这应该会导致语法错误。 , 之后不能有var。你为什么要用,而不是;来分隔你的语句? 您发布的任何代码都无法运行,所以我认为您没有正确复制它。请发布正确的代码。 jQuery(xml) 应该是 $xml。您的代码中没有变量 xml 而且你不需要再次调用jQuery(),你在分配$xml时把它变成了一个jQuery对象。 【参考方案1】:

正如 Barmar 的 cmets 所说,您的代码将无法运行,并且您似乎复制粘贴了错误的内容。但是,这些方面的内容应该可以满足您的需求。

代码本身的解释性很强,尽管有任何问题都可以提出来。

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 &amp; 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())
)
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

谢谢,我已经尝试过这段代码并且它可以工作,所以我尝试使用 $.get 获取我的 xml,这似乎是导致问题的原因。

以上是关于jQuery parseXML, .each()的主要内容,如果未能解决你的问题,请参考以下文章

jquery - $.each

JQuery 的$.each取值

jQuery - 使用“for-each”循环更改循环外的变量

jQuery

使用 jQuery each() 函数返回一个值

jquery之each()$.each()[jQuery.each()]