JavaScript 裝載XML到數組

來源:互聯網
上載者:User

 

 

/**//*
 * get all nodes's attribute and text
 */
function getAT(nodes)...{
    var XMLData=[];
    for(var i=0;i<nodes.length;i++)...{
        var crtNode=...{};
        crtNode.$name=nodes[i].nodeName;        
        if(nodes[i].attributes)...{
            for(var j=0;j<nodes[i].attributes.length;j++)...{
                debug.innerHTML+='<i>'+nodes[i].attributes[j].name+'='+nodes[i].attributes[j].value+'</i><br>';
                crtNode[nodes[i].attributes[j].name]=nodes[i].attributes[j].value;
            }
        }
        if(nodes[i].hasChildNodes())...{
            if(nodes[i].firstChild.nodeType==3)...{
                crtNode.$value=nodes[i].firstChild.nodeValue;
            }else...{
                crtNode.$value=null;
                var childNode=getAT(nodes[i].childNodes);
                debug.innerHTML+='<u>'+childNode[0].$name+'</u><br>';
                crtNode[childNode[0].$name]=childNode;
            }
        }
        XMLData.push(crtNode);
    }    
    return XMLData;
}
/**//*
 * Load XML into an array, each item of this array is an object. every object has at least two property: $name which is the xml node name and $value which is the text of the xml node(maybe null). 
 * @usage:
     xml=loadXML('game.xml');
 */
var XMLMSG='';// record error message, when a parse error ouccored
var _XML='';  // save xml text
var loadXML = function(xmlFile)
...{    
    var xmlDoc;
    if(window.ActiveXObject)
    ...{
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = false;
        xmlDoc.load(xmlFile);
    }
    else if (document.implementation&&document.implementation.createDocument)
    ...{
        xmlDoc = document.implementation.createDocument('', '', null);
        xmlDoc.load(xmlFile);
    }
    else
    ...{
        XMLMSG='Sorry, your browser doesn't support XML.'
        return false;
    }
    if(xmlDoc.parseError.errorCode!=0)...{
        XMLMSG+="<br/>Error Code: ";
        XMLMSG+=xmlDoc.parseError.errorCode;
        XMLMSG+="<br/>Error Reason: ";
        XMLMSG+=xmlDoc.parseError.reason;
        XMLMSG+="<br/>Error Line: ";
        XMLMSG+=xmlDoc.parseError.line;
        return false;
    }
    _XML=xmlDoc.documentElement.xml;
    var xmlNodes=xmlDoc.documentElement.childNodes;    
    debug.innerHTML+='<hr><font color="#ff0000">';
    return getAT(xmlNodes);    
}

 

參考文章:
javascript解析XML的方法 作者:luke 日期:2007-05-31 URL http://www.lukee.cn/article.asp?id=396

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.