XML 学习(三) DOM 解析- Node 对象的属性

XML 学习(三) DOM 解析- Node 对象的属性
下午在 W3C看了半天XML DOM 教程,弄了个例子,整理下;( W3C上有所有的网站建设教程,有一个对应的中文网站 http://www.w3school.com.cn/index.html,不过有些例子的连接打不开)
loadxmldoc.js,只有一个函数loadXMLDoc(dname),单数为解析XML文件名,返回一个XMLDOM对象;
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  测试页面
< 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 对象的属性有:
childNodes:返回某节点到子节点的节点列表
firstChild:返回某节点的首个子节点
lastChild:返回某个节点的最后一个子节点
nextSibling:返回某个节点之后紧跟的同级节点
nodeName:返回节点的名称,根据其类型
nodeType:返回节点的类型
nodeValue:设置或返回某个节点的值,根据其类型
ownerDocument:返回某个节点的根元素(document 对象)
parentNode:返回某节点的父节点
previousSibling:返回某个节点之前紧跟的同级节点
textContent:设置或返回某节点及其后代的文本内容
text:返回某节点及其后代的文本(IE 独有的属性)
xml:返回某节点及其后代的 XML(IE 独有的属性)

未测试或者不太明白的XML DOM - Node 对象的属性有,谁帮忙讲解下啊;
baseURI:返回某个节点的绝对基准
prefix:设置或返回某节点的命名空间前缀
localName:返回某个节点的本地名称
namespaceURI:返回某个节点的命名空间

你可能感兴趣的:(XML 学习(三) DOM 解析- Node 对象的属性)