下午在
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:返回某個節點的命名空間