XML到JavaScript数组与jQuery

前端之家收集整理的这篇文章主要介绍了XML到JavaScript数组与jQuery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 XML和AJAX的新手,只是 Javascript和jQuery的新手.除了其他工作职责我设计我们的网站.截止日期是非常接近的,我可以想到这样做的唯一方法是使用AJAX.我有一个充满XML对象的文档,如这一个重复:
  1. <item>
  2. <subject></subject>
  3. <date></date>
  4. <thumb></thumb>
  5. </item>

我想创建一个所有元素及其子元素的数组.我一直在阅读AJAX上的jQuery教程数小时,甚至不知道从哪里开始,因为它们都具有一定的JavaScript熟练程度.如果有人能告诉我最简单的方法来循环遍历所有的元素,并把他们的孩子放在一个数组中,我会很感激它.

使用jQuery,$.ajax()您的XML文件,并成功通过每个检索数据,如:
  1. var tmpSubject,tmpDate,tmpThumb;
  2. $.ajax({
  3. url: '/your_file.xml',type: 'GET',dataType: 'xml',success: function(returnedXMLResponse){
  4. $('item',returnedXMLResponse).each(function(){
  5. tmpSubject = $('subject',this).text();
  6. tmpDate = $('date',this).text();
  7. tmpThumb = $('thumb',this).text();
  8. //Here you can do anything you want with those temporary
  9. //variables,e.g. put them in some place in your html document
  10. //or store them in an associative array
  11. })
  12. }
  13. });

猜你在找的XML相关文章