XML将所有子项保存为变量

时间:2011-10-02 14:01:26

标签: jquery xml

我正在使用jQuery AJAX来获取XML文件:

$.ajax({
            type: "GET",
            url: phoneXmlPath,
            dataType: "xml",
            contentType: "text/xml; charset=utf-8",
            success: function(xml){
                $(xml).find("phone[phone_id="+phone_id+"]").each(function(index, value){
                    var tis = $(this);
                    tis.children().each(function(){
                        alert($(this).nodeName);
                    });
                }); 

            },
            error:function(xhr,type){
                if(type == null){
                    var errorMsg = "There was an error loading the phone.xml file!<br/>Please make sure the path to the xml file <strong>'"+phoneXmlPath+"'</strong> is correct.";
                }else if(type == "parsererror"){
                    var errorMsg = "There is an error in the file <strong>'"+phoneXmlPath+"'</strong>.";
                }else{
                    var errorMsg = "An error has been detected.";
                }       
                popup.removeClass("loading").html("<p><strong>Error:</strong> "+errorMsg+"</p><p><strong>Type of error:</strong> "+type+".</p>");       
            }           
        });

在这部分:alert($(this).nodeName);我怎样才能将所有内容保存到我以后可以访问的变量中。例如,我希望能够执行的代码:“phone.title”并访问所有手机子元素。

XML文件

<phones>
<phone>
    <phone_id>123</phone_id>
    <title>Phone title</title>
    etc....

1 个答案:

答案 0 :(得分:0)

使用JavaScript XML解析器 - http://www.w3schools.com/xml/xml_parser.asp

相关问题