使用Jquery从在线提要中解析XML

时间:2014-08-07 00:06:21

标签: jquery xml-parsing

如何从XML Feed中获取数据?看下面的示例:

http://jsfiddle.net/3se101uf/

$(document).ready(function () {
$.get('http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=thunderbay', function (d) {
    $('body').append('<dl />');

    $(d).find('route').each(function () {

        var $route = $(this);
        var tag = $route.find("tag").text();;
        var title = $route.find("title").text();;

        var html = '<dt> </dt>';
        html += '< p> ' + tag + ' - ' + title '< /p>';
        html += '</dd>';

        $('dl').append($(html));
        console.log(d);

    });
}); });

谢谢! :)

1 个答案:

答案 0 :(得分:0)

$(document).ready(function () {
    $.get('http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=thunderbay', function (xml) {
        $('route', xml).each(function (index, route) {
            var tag = $(route).attr('tag');
            var title = $(route).attr('title');

            $('#dl').append(
                $(document.createElement('dt'))
                    .append(
                        $(document.createElement('p'))
                            .text(tag + ' - ' + title)
                    )
            );
        });

    });
});

更新了小提琴here