Parsing xml in jQuery

前端 未结 2 395
南旧
南旧 2021-01-17 03:48

I have this xml file:

    Response: 

        
相关标签:
2条回答
  • 2021-01-17 04:13

    Have you received this XML file via an XMLHttpRequest? If so, you can use its responseXML property.

    alert(xhr.responseXML.documentElement.getAttribute("statusLocation"));
    

    Or with jQuery:

    $.ajax({
        type: "GET",
        url: "yourfile.xml",
        dataType: "xml",
        success: function(xml) {
            alert(xml.documentElement.getAttribute("statusLocation"));
        }
    });
    
    0 讨论(0)
  • 2021-01-17 04:22

    Here is one way to do it:

    var xml = null;
    function loadXML(myURL){
        $.ajax({
            type: "GET",
            url: myURL,
            dataType: ($.browser.msie) ? "text" : "xml",
            error: function(){
                return;
            },
            success: function(data){
                 if (typeof data == "string") {
                   xml = new ActiveXObject("Microsoft.XMLDOM");
                   xml.async = false;
                   xml.loadXML(data);
                    } else {
                   xml = data;
                 }
            }
        });
    };
    $(loadXML("common/glossary.xml"));
    

    then

    $(xml).find('something')
    
    0 讨论(0)
提交回复
热议问题