使用jQuery进行XML解析 [英] XML parsing using jQuery

查看:78
本文介绍了使用jQuery进行XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml:

<?xml version="1.0" encoding="utf-8"?>
<Area xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Scenes>
   <Scene Index="1" Name="Scene1" />
   <Scene Index="2" Name="Scene2" /> 
 </Scenes>
</Area>

我正在尝试使用jquery进行解析:

Which i am trying to parse with jquery:

<script>
        $(document).ready(function(){
            $.ajax({
                type: "GET",
                url: "list.xml",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find('scenes').each(function(){
                            $(this).find('scene').each(function(){
                            var name = $(this).attr('name');                            
                            $('<div class="items" ></div>').html('<p>'+name+'</p>').appendTo('#page-wrap'); 

                        });                     
                    });
                }
            });
        });
</script>

为什么这不起作用?帮助!!第一次尝试使用javascript/jquery 这是基于我发现的一个示例,但是到目前为止还无法使其适应我的用法. /拉尔斯

Why is this not working? Help!! first attempt at javascript/jquery This is based on a example I found, but have so far been unable to adapt it to my usage. / Lars

推荐答案

此代码在Safari和Firefox中对我有效:

This code works for me in Safari and (surprisingly) Firefox:

$.ajax({
    type: "GET",
    url: "list.xml",
    dataType: "xml",
    success: function(xml) {
  $(xml).find('Scenes').each(function(){
      $(this).find('Scene').each(function(){
          var name = $(this).attr('Name');                            
          $('<div class="items" ></div>').html('<p>'+name+'</p>').appendTo('#page-wrap'); 
             });                     
         });
    },
    error:function(a,b,c) { console.log( c ) }
});

它在某些浏览器中不起作用的原因可能是由于您是从文件系统托管的(假设您在).由于同源策略,通过AJAX请求访问文件系统时,Chrome和Firefox往往会遇到麻烦.

The reason it doesn't work in some browsers is likely due to the fact that you're hosting from the filesystem (assuming you are). Chrome and Firefox tend to give trouble when accessing the filesystem via AJAX request due to Same Origin Policy.

javascript很好.您只是得到一个空响应或一个错误.

The javascript is fine. You're just either getting an empty response, or an error.

此问题可能适用:

在Chrome中使用本地文件的jQuery getJSON问题

这篇关于使用jQuery进行XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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