jQuery-如何获取xml数据 [英] jquery - How to get xml data

查看:94
本文介绍了jQuery-如何获取xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对HTML,CSS,JavaScript和编程一无所知.请忍受我.

Im a total noob to html, css, javascript, and programming altogether. Please bear with me.

我正在尝试使用jquery填充我的表.数据将来自xml文件.

Im trying to populate my table using jquery. The data will be coming from an xml file.

football_player.xml:

<?xml version="1.0" encoding="UTF-8"?>

<football_player>
  <name>Cristiano Ronaldo</name>
  <club>Real Madrid</club>
  <number>7</number>
  <country>Portugal </country>

  <name>Fernando Torres </name>
  <club>Chelsea </club>
  <number>9</number>
  <country>Spain</country>

  <name>Iker Casillas</name>
  <club>Real Madrid </club>
  <number>1</number>
  <country>Spain</country>

  <name>David Beckham</name>
  <club>Los Angeles Galaxy</club>
  <number>23</number>
  <country>England</country>
</football_player>

我的html表:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Club</th>
      <th>Number</th>
      <th>Country</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
  </tfoot>
  </tfoot>
</table>

我的javascript/jquery脚本:

My javascript/jquery script:

$(document).ready(function () {
  $.ajax({
    type: "GET",
    url: "football_player.xml",
    dataType: "xml",
    success: function(xml) {
      $(xml).find("football_player").each(function () {
        $("table tbody").append("<tr>");
        $("table tbody").append("<td>" + $(this).find("name").text() + "</td>");
        $("table tbody").append("<td>" + $(this).find("club").text() + "</td>");
        $("table tbody").append("<td>" + $(this).find("number").text() + "</td>");
        $("table tbody").append("<td>" + $(this).find("country").text() + "</td>");
        $("table tbody").append("</tr>");           
      });
    }
  });
});

我发誓我真的是菜鸟.我不知道我在做什么.请帮忙.我真的很想学习.预先感谢.

I swear Im really a noob. I don't have any idea of what Im doing. Please help. I really want to learn. Thanks in advance.

推荐答案

示例XML:

<?xml version="1.0" encoding="utf-8" ?>
    <RecentBooks>
     <Book>
      <Title>My Cool Book Title</Title>
    <Description>The my cool book is possibly the best cool book in that any developer could use to     be a great web designer.</Description>
    <Date>12/1/2010</Date>
    </Book>
    <Book>
     <Title>Another PHP book</Title>
    <Description>Learn everything about PHP with 'Another PHP book,' your ultimate guide to the ins and outs of PHP.</Description>
    <Date>4/1/2010</Date>
    </Book>
    <Book>
    <Title>jQuery Techniques</Title>
    <Description>jQuery techniques runs you through real life examples of jQuery from beginner to expert</Description>
     <Date>6/2/2010</Date>
     </Book>
     <Book>
    <Title>MySQL Database Book</Title>
    <Description>Brush up your knowledge with the best MySQL database book on the market.          </Description>
    <Date>14/2/2010</Date>
    </Book>
    </RecentBooks>

和HTML& jQuery.

And the HTML & jquery.

$(document).ready(function () {
$.ajax({
    type: "GET",
    url: "books.xml",
    dataType: "xml",
    success: xmlParser
   });
});

function xmlParser(xml) {

$('#load').fadeOut();

$(xml).find("Book").each(function () {

    $(".main").append('<div class="book"><div class="title">' + $(this).find("Title").text() +   '</div><div class="description">' + $(this).find("Description").text() + '</div><div   class="date">Published ' + $(this).find("Date").text() + '</div></div>');
    $(".book").fadeIn(1000);

 });

}

<div class="main">
<div align="center" class="loader"><img src="loader.gif" id="load" width="16" height="11"   align="absmiddle"/></div>
</div>

 <div class="clear"></div>

您可以遍历该示例,并获得关于该示例的想法

you can go through the example and you will get idea about the same

这篇关于jQuery-如何获取xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆