首页 > 技术文章 > 【学习笔记】 XML DOM

flytu 2015-03-28 11:06 原文

以前学OC的时候觉得XML太简单了没什么内容,游戏资源加载要用到发现是被teacher忽悠了~~

还有一些没决绝的问题,特别是attributes,感觉不常用就先撂着,要知足常撂:)

吐槽下w3school,乱七八糟东拼西凑的,学习像在找东西!当然作为免费分享站点它还是很伟大的。

从上面找到的总结(总感觉漏了什么):

function loadXML(xmlf){
    var xmlHttp;
    var xmlDoc;
    var xmlDom;
    var xmlEle;
    var strXML = '<book>
                    <child>
                        <boy>
                        gaoda
                        </boy>
                        ailisi
                        <girl>
                        </girl>
                    </child>
                    <adult>
                        <man>
                        threebody
                        </man>
                        <woman>
                        moshenxiao
                        </woman>
                    </adult>
                  </book>';
    if(window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();//!IE

    }else if(window.ActiveXObject){
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');//IE
    }

    if(xmlHttp!=null){
        xmlHttp.onreadystatechange = state_Change;//for async true,complete to run,if false not need 
        xmlHttp.open('GET',xmlf,false);//GET or POST;xml or asp or PHP...;Async true or false
        xmlHttp.send(null);//GET null;POST xmlDoc
    }
    
    function state_Change(){
        if(xmlHttp.readyState == 4){//from 0 to 4
            if(xmlHttp.status == 200){//why not use &&?
                //do something
                xmlDom = xmlHttp.responseXML;//XML DOM
                xmlDoc = xmlHttp.responseText;//XML Document
                xmlEle = xmlDom.documentElement;//boot Node?
            }else{

            }
        }
    }

    //parser; XMLHttpRequest for net,parser for location?But XMLHttpRequest can load load location also
    if(window.DOMParser){//!IE
//         xmlDoc = document.implementation.creatDocument('','',null);//WTF
//         xmlDoc.async = 'false';
//         xmlDoc.load(xmlf);
//         var parser = new DOMParser();
//         xmlDoc = parser.parserFromString(strXML,'text/xml');        
    }else{//IE
//         xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
//         xmlDoc.async = 'false';//'false'?
//         xmlDoc.load(xmlf);//xml file,get DOM or DOC? 
//         xmlDoc.loadXML(strXML);//xml string             
    }

    //not find firstElementChild/children told by netbar
    //node
    var dom_0 = xmlDom.getElementsByTagName('')[0];//attention s
    var dom_1 = xmlDom.childNodes[1];
    var dom_2 = dom_0.firstChild;
    var dom_3 = dom_0.lastChild;
    var dom_4 = dom_2.nextSibling;
    var dom_5 = dom_3.previousSibling;
    var dom_6 = dom_3.parentNode;//dom_0

    var l_dom = dom_0.length;//IE<=!IE,#text #conmment in IE will be null but node in !IE

    document.write(dom_0.nodeName);//nodeName,read only
    document.write(dom_0.nodeType);//nodeType,from 1~9 element:1,read only
    document.write(dom_1.childNodes[0].nodeValue);//nodeValue,text in text Node
    //attribute
    var attributes = dom_0.attributes;//attributes,array
    var n_att = attributes.length;//not real length,real = l+8(8 format member)

    document.write(dom_2.getAttribute(''));//getAttribute
    getAttributeNode('');//difference from getAttribute?
    dom_2.getNamedItem('');//get attribute

    removeAttribute('');
    removeAttributeNode('');WTF
    

    dom_3.setAttribute('','');//setAttribute,if none creat,none addAttribute method
    xmlDom.createAttribute('')//
    dom_3.setAttributeNode('');//WTF

    xmlDom.createCDATAsection();//CDATA:<![CDATA[ something data ]]>
    xmlDom.createComment();//note <?--note--?>

    var newEle = xmlDom.createElement('');//createElement
    var newTex = xmlDom.createTextNode('');//createTextNode
    var newNod = dom_3.cloneNode(true);//cloneNode,true clone node and childNodes,false only node;

    newEle.appendChild(newTex);//appendChild to Node(new Node)
    dom_3.appendChild(newEle);//appendChild to DOM(exist Node),would it change XML?
    dom_0.insertBefore(dom_1,newEle);//insertBefore
    dom_0.removeChild(dom_2);//removeChild
    dom_0.replaceChild(dom_4);//replace Node
    dom_1.childNodes[0].replaceData(,,'');//position,length,replace text node value
    dom_1.childNodes[0].insertData(,'');//position,str
     
}
//E4X so easy
ver xmlDoc =new XML();//xmlDoc =new XML(strXML);
xmlDoc.load(file.xml);

AJAX待补充~~

尝试下以后代码里面不用chinese了,还很幼稚的comment~~~Can you understand yourself??

推荐阅读