xml数据报废比我更先进

时间:2016-08-11 05:03:54

标签: javascript jquery ajax xml html5

感谢您的光临并了解我是新手。所以对我来说似乎很容易对我来说很新鲜。

我做了一些关于如何使用javascript和/或php从.xml文件中提取数据的研究。我也可以使用html5 canvas(Construct 2)将这个.xml数据拉入移动应用程序

我的问题与节点本身无关,但后来又是。

如果节点有这样的文本就更容易了。

<item>Hi There</item>

而不是像我想要的那样的子节点。

<item game="powerball" nextjp="$67 Million" nextdd="Wednesday, August 10, 2016" winnum="20-33-36-47-52 PB12 X3" windd="Saturday, August 6, 2016" myflv="widget_pb.flv" winnumNM="20-33-36-47-52 PB12" name="POWERBALL">

我想分解节点,使其读取

Game
Next Jackpot
Next drawing
Winning Numbers
Game Played Date

如果我能找到一个已经拥有每个节点的xml而不是一个节点内的所有节点,那对我来说很容易。因此,我说我不需要完整的详细脚本演示,我只需要了解如何获取子节点数据。谢谢Jeremy C.

1 个答案:

答案 0 :(得分:0)

在这种情况下,我从字符串中加载了整个XML。在实际情况中,您可能需要使用ajax来拉它。

注意:您在评论中添加的URL,我从中获取此xml并不具有“允许*来源标题&#34;所以,那里的ajax会产生CORS错误。

&#13;
&#13;
var xml = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>Florida Lottery Winning Numbers</title><link>http://www.flalottery.com</link><description>Florida Lottery winning number drawing results for  FLORIDA LOTTO, MEGA MILLIONS, POWERBALL, LUCKY MONEY, FANTASY 5, Midday and Evening PICK 2, PICK3, PICK 4 and PICK 5</description><item game="powerball" nextjp="$67 Million" nextdd="Wednesday, August 10, 2016" winnum="20-33-36-47-52 PB12 X3" windd="Saturday, August 6, 2016" myflv="widget_pb.flv" winnumNM="20-33-36-47-52 PB12" name="POWERBALL"><title>POWERBALL Drawing Results</title><link>http://www.flalottery.com/powerball.do</link><description>POWERBALL winning numbers are 20-33-36-47-52 PB12 X3 for 08/06/2016 and the next estimated Jackpot is $67 Million for 08/10/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/powerball.do</guid></item><item game="lotto" nextjp="$12 Million" nextdd="Wednesday, August 10, 2016" winnum="1-22-27-40-42-53 X3" windd="Saturday, August 6, 2016" myflv="widget_l6.flv" name="FLORIDA LOTTO"><title>FLORIDA LOTTO Drawing Results</title><link>http://www.flalottery.com/lotto.do</link><description>FLORIDA LOTTO with Xtra winning numbers are 1-22-27-40-42-53 X3 for 08/06/2016 and the next Jackpot is $12 Million for 08/10/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/lotto.do</guid></item><item game="fan5" winnum="3-23-25-33-34" windd="Tuesday, August 9, 2016" myflv="widget_f5.flv" name="FANTASY 5"><title>FANTASY 5 Drawing Results</title><link>http://www.flalottery.com/fantasy5.do</link><description>FANTASY 5 winning numbers are 3-23-25-33-34 for 08/09/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/fantasy5.do</guid></item><item game="pick5" winnumm="6-7-9-1-9" midd="Wednesday, August 10, 2016" winnume="4-7-1-9-1" eved="Wednesday, August 10, 2016" myflvm="NA" myflve="NA" name="PICK 5"><title>PICK 5 Drawing Results</title><link>http://www.flalottery.com/pick5.do</link><description>PICK 5 winning numbers are 6-7-9-1-9 for Midday 08/10/2016 and  4-7-1-9-1 for Evening 08/10/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/pick5.do</guid></item><item game="pick4" winnumm="3-6-4-7" midd="Wednesday, August 10, 2016" winnume="7-1-1-7" eved="Wednesday, August 10, 2016" myflvm="widget_p4_midd.flv" myflve="widget_p4_eved.flv" name="PICK 4"><title>PICK 4 Drawing Results</title><link>http://www.flalottery.com/pick4.do</link><description>PICK 4 winning numbers are 3-6-4-7 for Midday 08/10/2016 and  7-1-1-7 for Evening 08/10/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/pick4.do</guid></item><item game="pick3" winnumm="3-5-0" midd="Wednesday, August 10, 2016" winnume="2-4-8" eved="Wednesday, August 10, 2016" myflvm="widget_c3_midd.flv" myflve="widget_c3_eved.flv" name="PICK 3"><title>PICK 3 Drawing Results</title><link>http://www.flalottery.com/pick3.do</link><description>PICK 3 winning numbers are 3-5-0 for Midday 08/10/2016 and  2-4-8 for Evening 08/10/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/pick3.do</guid></item><item game="pick2" winnumm="9-6" midd="Wednesday, August 10, 2016" winnume="7-7" eved="Wednesday, August 10, 2016" myflvm="NA" myflve="NA" name="PICK 2"><title>PICK 2 Drawing Results</title><link>http://www.flalottery.com/pick2.do</link><description>PICK 2 winning numbers are 9-6 for Midday 08/10/2016 and  7-7 for Evening 08/10/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/pick2.do</guid></item><item game="megamillions" nextjp="$45 Million" nextdd="Friday, August 12, 2016" winnum="12-19-20-44-66 MM1 X5" windd="Tuesday, August 9, 2016" name="MEGA MILLIONS"><title>MEGA MILLIONS Drawing Results</title><link>http://www.flalottery.com/megaMillions.do</link><description>MEGA MILLIONS winning numbers are 12-19-20-44-66 MM1 X5 for 08/09/2016 and the next estimated Jackpot is $45 Million for 08/12/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/megaMillions.do</guid></item><item game="lucky" nextjp="$700,000" nextdd="Friday, August 12, 2016" winnum="1-11-27-39 LB3" windd="Tuesday, August 9, 2016" myflv="widget_lucky.flv" name="LUCKY MONEY"><title>LUCKY MONEY Drawing Results</title><link>http://www.flalottery.com/luckyMoney.do</link><description>LUCKY MONEY winning numbers are 1-11-27-39 LB3 for 08/09/2016 and the next Jackpot is $700,000 for 08/12/2016</description><pubDate>Wed, 10 Aug 2016 22:11:46 EST</pubDate><guid>http://www.flalottery.com/luckyMoney.do</guid></item></channel></rss>';

var xmlDoc = $.parseXML(xml),
  $xml = $(xmlDoc),
  $data = $xml.find("item");
var res = "";
$data.each(function() {
  var str = "<tr>";
  str += "<td>" + ($(this).attr('game')) + "</td>";
  str += "<td>" + ($(this).attr('nextjp')) + "</td>";
  str += "<td>" + ($(this).attr('nextdd')) + "</td>";
  str += "<td>" + ($(this).attr('winnum')) + "</td>";
  str += "<td>" + ($(this).attr('windd')) + "</td>";
  str += "</tr>";
  res += str;
});
$("#output").append(res);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="output" border="1">
  <tr>
    <th>Game</th>
    <th>Next Jackpot</th>
    <th>Next drawing</th>
    <th>Winning Numbers</th>
    <th>Game Played Date</th>
  </tr>
</table>
&#13;
&#13;
&#13;