Rookie

          Headache English

          數(shù)據(jù)加載中……
          XML 學(xué)習(xí)(三) DOM 解析- Node 對(duì)象的屬性
          下午在W3C看了半天XML DOM 教程,弄了個(gè)例子,整理下;(W3C上有所有的網(wǎng)站建設(shè)教程,有一個(gè)對(duì)應(yīng)的中文網(wǎng)站http://www.w3school.com.cn/index.html,不過(guò)有些例子的連接打不開)
          loadxmldoc.js,只有一個(gè)函數(shù)loadXMLDoc(dname),單數(shù)為解析XML文件名,返回一個(gè)XMLDOM對(duì)象;
          function loadXMLDoc(dname)
          {
              var xmlDoc;
              
          // code for IE
              if (window.ActiveXObject)
              {
                  xmlDoc
          =new ActiveXObject("Microsoft.XMLDOM");
              }
              
          // code for Mozilla, Firefox, Opera, etc.
              else if (document.implementation && document.implementation.createDocument)
              {
                  xmlDoc
          =document.implementation.createDocument("","",null);
              }
              
          else
              {
                  alert(
          'Your browser cannot handle this script');
              }
              xmlDoc.async
          =false;
              xmlDoc.load(dname);
              
          return(xmlDoc);
          }
          book.xml待解析XML文件
          <?xml version="1.0" encoding="ISO-8859-1" ?> 
          <!--  Edited with XML Spy v2007 (http://www.altova.com) --> 
          <bookstore>
            
          <book category="COOKING">
            
          <title lang="en">Everyday Italian</title> 
            
          <author>Giada De Laurentiis</author> 
            
          <year>2005</year> 
            
          <price>30.00</price> 
            
          </book>
            
          <book category="CHILDREN">
            
          <title lang="en">Harry Potter</title> 
            
          <author>J K. Rowling</author> 
            
          <year>2005</year> 
            
          <price>29.99</price> 
            
          </book>
            
          <book category="WEB">
            
          <title lang="en">XQuery Kick Start</title> 
            
          <author>James McGovern</author> 
            
          <author>Per Bothner</author> 
            
          <author>Kurt Cagle</author> 
            
          <author>James Linn</author> 
            
          <author>Vaidyanathan Nagarajan</author> 
            
          <year>2003</year> 
            
          <price>49.99</price> 
            
          </book>
            
          <book category="WEB">
            
          <title lang="en">Learning XML</title> 
            
          <author>Erik T. Ray</author> 
            
          <year>2003</year> 
            
          <price>39.95</price> 
            
          </book>
          </bookstore>
          book.html  測(cè)試頁(yè)面
          <html>
              
          <head>
                  
          <script src="../loadxmldoc.js"></script>
                  
          <script>
                      
          var xmlDoc = loadXMLDoc("book.xml");
                      
          function getFirstChild(doc){
                               
          var x = doc.firstChild;
                               
          while(x.nodeType!=1){
                                       x.nextSibling;
                               }
                               
          return x;
                       }
                       
          function getLastChild(doc){
                               
          var x = doc.lastChild;
                               
          while(x.nodeType!=1){
                                       x.previousSibling;
                               }
                               
          return x;
                       }
                       
                      
          var first = getFirstChild(xmlDoc.documentElement);
                      document.write(
          "first.nodeName:" + first.nodeName);
                      document.write(
          "first.nodeType:" + first.nodeType + "<br/><br/>");

                      
          var last = getLastChild(xmlDoc.documentElement);
                      document.write(
          "last.nodeName:" + last.nodeName);
                      document.write(
          "last.nodeType:" + last.nodeType + "<br/><br/>");

                      
          var test = xmlDoc.getElementsByTagName("title");
                      
          var parent = test.item(0).parentNode;
                      document.write(
          "parent.nodeName:" + parent.nodeName + "<br/><br/>");
                      document.write(
          "textContent:" + parent.textContent + "<br/><br/>");
                      document.write(
          "text:" + parent.text + "<br/><br/>");
                      document.write(
          "xml:" + "<xmp>" + parent.xml + "</xmp>" + "<br/><br/>");
                                  
                      document.write(xmlDoc.nodeName);
                      document.write(xmlDoc.nodeType 
          + "  ");
                      document.write(xmlDoc.childNodes[
          0].nodeValue + "<br/>");            
                      
          var x = xmlDoc.documentElement;//獲得xml文件文檔元素,即bookstore
                      document.write(x.nodeName);
                      document.write(x.nodeType 
          + "  ");
                      document.write(x.childNodes.item(
          0).nodeValue + "<br/>");            
                      
          var child = x.childNodes;//獲得 bookstore所有的子元素 book
                      //顯示bookstore所有元素
                      for(i=0; i< child.length; i++){                
                          document.write(child[i].nodeName);
                          document.write(child[i].nodeType 
          + "  ");    
                          document.write(child[i].childNodes[
          0].nodeValue + "<br/>");
                          
          var ch = child[i];
                          
          for(j=0; j<ch.childNodes.length; j++){
                              document.write(ch.childNodes[j].nodeName);
                              document.write(ch.childNodes[j].nodeType 
          + "  ");                        
                              document.write(ch.childNodes[j].childNodes[
          0].nodeValue + "<br/>");
                          }
                      }            
                  
          </script>
              
          </head>
          </html>

          其中用到的XML DOM - Node 對(duì)象的屬性有:
          childNodes:返回某節(jié)點(diǎn)到子節(jié)點(diǎn)的節(jié)點(diǎn)列表
          firstChild:返回某節(jié)點(diǎn)的首個(gè)子節(jié)點(diǎn)
          lastChild:返回某個(gè)節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)
          nextSibling:返回某個(gè)節(jié)點(diǎn)之后緊跟的同級(jí)節(jié)點(diǎn)
          nodeName:返回節(jié)點(diǎn)的名稱,根據(jù)其類型
          nodeType:返回節(jié)點(diǎn)的類型
          nodeValue:設(shè)置或返回某個(gè)節(jié)點(diǎn)的值,根據(jù)其類型
          ownerDocument:返回某個(gè)節(jié)點(diǎn)的根元素(document 對(duì)象)
          parentNode:返回某節(jié)點(diǎn)的父節(jié)點(diǎn)
          previousSibling:返回某個(gè)節(jié)點(diǎn)之前緊跟的同級(jí)節(jié)點(diǎn)
          textContent:設(shè)置或返回某節(jié)點(diǎn)及其后代的文本內(nèi)容
          text:返回某節(jié)點(diǎn)及其后代的文本(IE 獨(dú)有的屬性)
          xml:返回某節(jié)點(diǎn)及其后代的 XML(IE 獨(dú)有的屬性)

          未測(cè)試或者不太明白的XML DOM - Node 對(duì)象的屬性有,誰(shuí)幫忙講解下啊;
          baseURI:返回某個(gè)節(jié)點(diǎn)的絕對(duì)基準(zhǔn)
          prefix:設(shè)置或返回某節(jié)點(diǎn)的命名空間前綴
          localName:返回某個(gè)節(jié)點(diǎn)的本地名稱
          namespaceURI:返回某個(gè)節(jié)點(diǎn)的命名空間

          posted on 2008-01-09 21:05 zhhang920 閱讀(1149) 評(píng)論(0)  編輯  收藏 所屬分類: JAVASCRIPT

          主站蜘蛛池模板: 巴彦淖尔市| 双峰县| 额济纳旗| 台湾省| 德江县| 沐川县| 富蕴县| 柳州市| 竹北市| 舞钢市| 邛崃市| 石河子市| 扎鲁特旗| 三门县| 临猗县| 盘锦市| 托克托县| 白城市| 海晏县| 翁牛特旗| 竹北市| 兴文县| 尉氏县| 南陵县| 兰坪| 嘉禾县| 芦溪县| 东城区| 永川市| 遂川县| 丰台区| 邵阳市| 横山县| 通辽市| 宿州市| 北海市| 江北区| 墨脱县| 澎湖县| 牡丹江市| 兴安县|