首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery parseXML,.each()

jQuery parseXML,.each()
EN

Stack Overflow用户
提问于 2021-01-14 07:33:10
回答 1查看 31关注 0票数 0

我正在尝试使用jquery解析我的xml以创建表。我有下面的代码。我试图让它在标签中循环,但是循环不起作用,我认为循环中的任何东西都没有运行。我有80%的把握我的xml是正确的,因为当查找任何标记时,如果没有.each(),它将输出所有但不是分开的。有人对我做错了什么有什么建议吗?

代码语言:javascript
复制
$(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文件。

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2021-01-14 07:52:21

正如Barmar的评论所说,您的代码将不会运行,并且看起来您可能复制粘贴了错误的内容。但是,这些内容应该可以满足您的需求。

代码本身很有说明性,尽管您可以随时询问有关它的问题。

代码语言:javascript
复制
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())
})
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65711333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档