有问题使用jquery解析xml

时间:2012-01-19 03:13:16

标签: jquery xml

我遇到了一些xml解析的问题。我希望数据输出如下:

<ul id="schedule">
   <li><span class="title">Title</span>
       <span class="date">Date</span>
       <span class="desc">Description</span>
   </li>
</ul>

和jquery

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

      function parseXml(xml)
        {
          $(xml).find('item').each(function() {
                var title = $(this).find('title').text();
                var date  = $(this).find('date').text();
                var desc  = $(this).find('description').text();

                $('#schedule').append($('<li><span class="'title'">' + title + '</span>' + ***the other elements i can't escape the 's***</li>'));
            });
        }
    });

最后是xml

<?xml version="1.0" encoding="utf-8"?>
<schedule>
    <item>
        <title>Event Name</title>
        <date>18 Jan 2012 09:00 MST</date>
        <description>Aliquam et nisl vel ligula consectetuer suscipit. Morbi euismod enim eget</description>
    </item>
</schedule>

我知道这很简单,但它让我发疯。谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个 HTML内容

<body>
 <ul id="schedule">
 </ul>  
</body>

jquery的

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

          function parseXml(xml)
            {
              $(xml).find('item').each(function() {
                    var title = $(this).find('title').text();
                    var date  = $(this).find('date').text();
                    var desc  = $(this).find('description').text();             
                    var cont = $('<li><span class="title">'+title+'</span>'+'<span class="date">'+date+'<span class="desc"></li>');

                    cont.appendTo('#schedule');

                });
            }
        });