简体   繁体   English

可以使用jquery .ajax()调用解析包含xml数据的javascript对象吗?

[英]Can a javascript object containing xml data be parsed using the jquery .ajax() call?

I have a javascript object containing the xml data in that. 我有一个javascript对象,其中包含xml数据。 I want to parse this object inside the .ajax() call in the jquery. 我想在jquery中的.ajax()调用中解析此对象。 Does anyone know how to do it? 有人知道怎么做吗? I am struggling for the pointer. 我正在努力寻找指针。 Please help me with this. 请帮我解决一下这个。

Thanks! 谢谢!

Use jQuery.parseXML( data ); 使用jQuery.parseXML( data );

See docs here: http://api.jquery.com/jQuery.parseXML/ 在这里查看文档: http : //api.jquery.com/jQuery.parseXML/

Here's an example of parsing a catalog of books contained in book tags and caching results into a javascript object for much easier access to data later on. 这是一个示例,该示例分析book标记中包含的book目录,并将结果缓存到javascript对象中,以便以后更轻松地访问数据。 XML Sample taken from MSDN site MSDN站点获取的XML示例

This allows a lot easier data access later in than having to parse the xml again to look for results. 与以后必须再次解析xml以查找结果相比,这使以后的数据访问更加容易。 The format of the store object can be set to best suit needs of the app this way also. 也可以通过这种方式将商店对象的格式设置为最适合应用程序的需求。

DEMO : http://jsfiddle.net/WpFUE/ 演示: http : //jsfiddle.net/WpFUE/

var xmlResults = {};

$(xml).find('book').each(function() {
    var $book = $(this);
    var id = $book.attr('id');
    var title = $book.find('title').text();
    var auth = $book.find('author').text();
    var descrip = $book.find('description').text();
    /* store data in object with id for key */
    xmlResults[id] = {
        author: auth
    }

    $('body').append('<div class="book_wrap" data-id="' + id + '">Book: ' + title + '<br>' + descrip + '<p>CLICK ANYEHWERE ON DIV TO GET AUTHOR</div>')

})

$('.book_wrap').click(function() {
    var id = $(this).data('id');
    /* no parsing of xml, use simple javascript object notation to referece data stored*/
    var auth = xmlResults[id].author;
    alert('Author is '+auth)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM