开发者

XML save all children to variable

开发者 https://www.devze.com 2023-04-09 18:51 出处:网络
I am using jQuery AJAX to get a XML file with: $.ajax({ type: \"GET\", url: phoneXmlPath, dataType: \"xml\",

I am using jQuery AJAX to get a XML file with:

$.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"){
           开发者_JS百科         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>");       
            }           
        });

In this part: alert($(this).nodeName); how could I save everything to a variables that I can later access. E.g further down the code I would like to be able to do: "phone.title" and access all of the phone children elements.

XML file

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


Use the JavaScript XML parser - http://www.w3schools.com/xml/xml_parser.asp

0

精彩评论

暂无评论...
验证码 换一张
取 消