jQuery XML解析-获取属性值 [英] jQuery XML parsing - Getting attribute values

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

问题描述

通过Ajax发送请求后,我收到以下xml响应,因此我想使用 jQuery 解析以下 xml 数据以获取特定的属性值.

I am getting this following xml response after sending requests through Ajax, so i want to parse this following xml data using jQuery to get the specific attribute values.

<Room1>
            <Node Name='Scene1' Text='Morning'/>
            <Node Name='Scene2' Text='Leaving'/>
            <Node Name='Scene3' Text='Morning'/>
            <Node Name='Scene4' Text='Entertainment'/>
            <Node Name='Scene5' Text='Party'/>
            <Node Name='Scene6' Text='s6'/>
            <Node Name='Scene7' Text='Watch TV'/>
            <Node Name='Scene8' Text='Watch Movie'/>
            <Node Name='Scene9' Text='TV on Screen'/>
            <Node Name='Scene10' Text='Movie on Screen'/>
            <Node Name='Scene11' Text='Leaving Room'/>
</Room1>

我想获取属性名称文本的值.以下代码是我尝试过的,

I want to get the values of attributes Name and Text. The following code is what i have tried,

 $.ajax({
            method : "GET",
            url    :  "controller/Controller.php",
            data   : "myData=Room1",
            success:function(response){
                    var xml = response;

                    $(xml).find('Room1').each(function(){
                        $(this).find('Node').each(function(){
                            console.log($(this).attr('Name'));
                        });
                    });
                }
        });

预先感谢

推荐答案

我建议您将整个xml转换为json,然后在jQuery中使用对象时就可以使用json.

I would suggest you convert the whole xml into json, and then you can use json as you use objects in jquery.

用于将XML序列化为JSON的jQuery插件

例如,获取名称和文本属性,您要做的就是:

e.g to get to name and text attribute, all you would do is:

$.each(jsonObject,function(index){console.log($(this).name)}

$.each(jsonObject, function(index) { console.log($(this).name) }

这篇关于jQuery XML解析-获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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