Javascript里使用Dom操作Xml
更新時(shí)間:2007年01月22日 00:00:00 作者:
看了一天的XML資料,感覺(jué)CSDN上這篇講的挺細(xì)致的。即有Dot Net寫(xiě)入XML文件的示例,又有JS讀取的示例,值得一看。(Source:http://blog.csdn.net/flypigluo)
一.本筆記使用的Xml文件
二.IXMLDOMDocument/DOMDocument簡(jiǎn)介
2.1 屬性
2.1.1 parseError
2.1.2 async.
2.1.3 xml
2.1.4 text3
2.1.5 attributes
2.1.6 nodeName
2.1.7 documentElement
2.1.8 nextSibling
2.1.9 childNodes
2.1.10 firstChild
2.1.11 lashChild
2.2 方法
2.2.1 loadXML
2.2.2 load
2.2.3 selectSingleNode
2.2.4 selectNodes
2.2.5 getElementsByTagName
2.2.6 hasChildNodes
三.例子
一.本筆記使用的Xml文件
<?xml version="1.0"?>
<book level="1"> <Name>c++</Name>
<Price>20</Price>
<info>
<k>1</k>
</info>
<info>
<k>2</k>
</info>
</book>
在asp.net下實(shí)現(xiàn)代碼:
string str = Server.MapPath("test1.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(str,null);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.WriteStartDocument(); mlWriter.WriteStartElement("book");
xmlWriter.WriteAttributeString("level","1");
xmlWriter.WriteElementString("Name","c++");
xmlWriter.WriteElementString("Price","20");
xmlWriter.WriteStartElement("info");
xmlWriter.WriteElementString("k","1");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("info");
xmlWriter.WriteElementString("k","2");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
二.IXMLDOMDocument/DOMDocument簡(jiǎn)介
2.1 屬性
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error
返回解析錯(cuò)誤時(shí)的一個(gè)對(duì)象。
重要的有parseError.errorCode,parseError.reason
如果load時(shí)路徑不對(duì),會(huì)返回“系統(tǒng)未找到指定的對(duì)象”的錯(cuò)誤
2.1.2 async
Specifies whether asynchronous download is permitted
是否允許異步下載,布爾值
2.1.3 xml
Contains the XML representation of the node and all its descendants. Read-only.
該點(diǎn)及下面派生的所有點(diǎn)的全部信息,只讀如果要求book點(diǎn)的xml,返回“<book level="1"><Name>c++</Name><Price>20</Price><info><k>1</k></info><info><k>2</k></info></book>”,如果Name的xml,返回“<Name>c++</Name>”
2.1.4 text
Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write
該點(diǎn)及下面派生的所有點(diǎn)的全部節(jié)點(diǎn)值,可讀可寫(xiě)
<price>20</price>
則text為20
"Name"節(jié)點(diǎn)的text為"c++"
2.1.5 attributes
Contains the list of attributes for this node
返回屬性的集合。
2.1.6 nodeName
Returns the qualified name for attribute, document type, element, entity, or notation nodes. Returns a fixed string for all
other node types. Read-only
該節(jié)點(diǎn)名稱(chēng)
"Name"節(jié)點(diǎn)的nodeName為"Name","book"節(jié)點(diǎn)的nodeName為"book"
2.1.7 documentElement
Contains the root element of the document
xml的根節(jié)點(diǎn)
上面的xml的根節(jié)點(diǎn)為"book"
2.1.8 nextSibling
Contains the next sibling of the node in the parent's child list. Read-only.
下一個(gè)兄弟節(jié)點(diǎn),只讀
2.1.9 childNodes
Contains a node list containing the child nodes
所有的子節(jié)點(diǎn)。
2.1.10 firstChild
Contains the first child of the node
第一個(gè)子節(jié)點(diǎn)
2.1.11 lastChild
Returns the last child node
最后一個(gè)子節(jié)點(diǎn)
2.2 方法
2.2.1 loadXML
Loads an XML document using the supplied string
2.2.2 load
Loads an XML document from the specified locati
參數(shù)的路徑為服務(wù)器端的,是相對(duì)路徑
2.2.3 selectSingleNode
Applies the specified pattern-matching operation to this node's context and returns the first matching node
返回第一個(gè)匹配的項(xiàng)
2.2.4 selectNodes
Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList
符合條件的所有項(xiàng)。
2.2.5 getElementsByTagName
Returns a collection of elements that have the specified name
返回與元素名匹配的一個(gè)node的集合
2.2.6 hasChildNodes
Provides a fast way to determine whether a node has children
判斷是否含有子節(jié)點(diǎn)
返回值為bool值
三.例子
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.load("test\\test1.xml");
if (xmlDoc.parseError.errorCode!=0)
{
var error = xmlDoc.parseError;
alert(error.reason)
return;
}
var root = xmlDoc.documentElement; //根節(jié)點(diǎn)
Form1.test1.value = root.xml;
/*結(jié)果如下:
<book level="1"><Name>c++</Name><Price>20</Price><info><k>1</k></info><info><k>2</k></info></book>*/
Form1.test1.value = root.nodeName; //結(jié)果為"book"
var att = root.attributes; //得到該點(diǎn)下所有屬性的集合
var str = "";
for (var i=0; i<att.length; i++)
{
str += att.item(i).nodeName+":"+att.item(i).text;
}
Form1.test1.value = str; //只有一個(gè)屬性,所以結(jié)果為“l(fā)evel:1”
var fNode;
var lNode;
var nextSibling;
fNode = root.firstChild; //第一個(gè)子節(jié)點(diǎn)Name
lNode = root.lastChild; //最后一個(gè)子節(jié)點(diǎn) info
nextSibling = fNode.nextSibling; //第一個(gè)子節(jié)點(diǎn)Name的后一個(gè)兄弟節(jié)點(diǎn),即Price
str = fNode.nodeName + ":" + fNode.text; //結(jié)果:"Name:c++"
str = lNode.nodeName + ":" + lNode.text; //結(jié)果為:"info:2"
str = nextSibling.nodeName + ":" + nextSibling.text; //結(jié)果為:"Price:20"
var nodeList;
str = "";
nodeList = xmlDoc.selectNodes("http://info"); //查找元素名為"info"的節(jié)點(diǎn)
for (var j=0; j<nodeList.length; j++) //有兩個(gè)info節(jié)點(diǎn)
{
var infoNode = nodeList.item(j);
var cldNodes = infoNode.childNodes; //info節(jié)點(diǎn)的子節(jié)點(diǎn)集
for (var k=0; k<cldNodes.length; k++)
{
str += cldNodes.item(k).nodeName + ":" + cldNodes.item(k).text + " ";
}
//結(jié)果“k:1 k:2 ”
}
str = "";
var sNode;
sNode = xmlDoc.selectSingleNode("http://info"); //找到第一個(gè)和"info"匹配的
var scldNodes = sNode.childNodes; //info節(jié)點(diǎn)的子節(jié)點(diǎn)集
for (var t=0; t<scldNodes.length; t++)
{
str += scldNodes.item(t).nodeName + ":" + scldNodes.item(t).text + " ";
}
//結(jié)果“k:1”
Form1.test1.value = str;
一.本筆記使用的Xml文件
二.IXMLDOMDocument/DOMDocument簡(jiǎn)介
2.1 屬性
2.1.1 parseError
2.1.2 async.
2.1.3 xml
2.1.4 text3
2.1.5 attributes
2.1.6 nodeName
2.1.7 documentElement
2.1.8 nextSibling
2.1.9 childNodes
2.1.10 firstChild
2.1.11 lashChild
2.2 方法
2.2.1 loadXML
2.2.2 load
2.2.3 selectSingleNode
2.2.4 selectNodes
2.2.5 getElementsByTagName
2.2.6 hasChildNodes
三.例子
一.本筆記使用的Xml文件
<?xml version="1.0"?>
<book level="1"> <Name>c++</Name>
<Price>20</Price>
<info>
<k>1</k>
</info>
<info>
<k>2</k>
</info>
</book>
在asp.net下實(shí)現(xiàn)代碼:
string str = Server.MapPath("test1.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(str,null);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.WriteStartDocument(); mlWriter.WriteStartElement("book");
xmlWriter.WriteAttributeString("level","1");
xmlWriter.WriteElementString("Name","c++");
xmlWriter.WriteElementString("Price","20");
xmlWriter.WriteStartElement("info");
xmlWriter.WriteElementString("k","1");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("info");
xmlWriter.WriteElementString("k","2");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
二.IXMLDOMDocument/DOMDocument簡(jiǎn)介
2.1 屬性
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error
返回解析錯(cuò)誤時(shí)的一個(gè)對(duì)象。
重要的有parseError.errorCode,parseError.reason
如果load時(shí)路徑不對(duì),會(huì)返回“系統(tǒng)未找到指定的對(duì)象”的錯(cuò)誤
2.1.2 async
Specifies whether asynchronous download is permitted
是否允許異步下載,布爾值
2.1.3 xml
Contains the XML representation of the node and all its descendants. Read-only.
該點(diǎn)及下面派生的所有點(diǎn)的全部信息,只讀如果要求book點(diǎn)的xml,返回“<book level="1"><Name>c++</Name><Price>20</Price><info><k>1</k></info><info><k>2</k></info></book>”,如果Name的xml,返回“<Name>c++</Name>”
2.1.4 text
Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write
該點(diǎn)及下面派生的所有點(diǎn)的全部節(jié)點(diǎn)值,可讀可寫(xiě)
<price>20</price>
則text為20
"Name"節(jié)點(diǎn)的text為"c++"
2.1.5 attributes
Contains the list of attributes for this node
返回屬性的集合。
2.1.6 nodeName
Returns the qualified name for attribute, document type, element, entity, or notation nodes. Returns a fixed string for all
other node types. Read-only
該節(jié)點(diǎn)名稱(chēng)
"Name"節(jié)點(diǎn)的nodeName為"Name","book"節(jié)點(diǎn)的nodeName為"book"
2.1.7 documentElement
Contains the root element of the document
xml的根節(jié)點(diǎn)
上面的xml的根節(jié)點(diǎn)為"book"
2.1.8 nextSibling
Contains the next sibling of the node in the parent's child list. Read-only.
下一個(gè)兄弟節(jié)點(diǎn),只讀
2.1.9 childNodes
Contains a node list containing the child nodes
所有的子節(jié)點(diǎn)。
2.1.10 firstChild
Contains the first child of the node
第一個(gè)子節(jié)點(diǎn)
2.1.11 lastChild
Returns the last child node
最后一個(gè)子節(jié)點(diǎn)
2.2 方法
2.2.1 loadXML
Loads an XML document using the supplied string
2.2.2 load
Loads an XML document from the specified locati
參數(shù)的路徑為服務(wù)器端的,是相對(duì)路徑
2.2.3 selectSingleNode
Applies the specified pattern-matching operation to this node's context and returns the first matching node
返回第一個(gè)匹配的項(xiàng)
2.2.4 selectNodes
Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList
符合條件的所有項(xiàng)。
2.2.5 getElementsByTagName
Returns a collection of elements that have the specified name
返回與元素名匹配的一個(gè)node的集合
2.2.6 hasChildNodes
Provides a fast way to determine whether a node has children
判斷是否含有子節(jié)點(diǎn)
返回值為bool值
三.例子
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.load("test\\test1.xml");
if (xmlDoc.parseError.errorCode!=0)
{
var error = xmlDoc.parseError;
alert(error.reason)
return;
}
var root = xmlDoc.documentElement; //根節(jié)點(diǎn)
Form1.test1.value = root.xml;
/*結(jié)果如下:
<book level="1"><Name>c++</Name><Price>20</Price><info><k>1</k></info><info><k>2</k></info></book>*/
Form1.test1.value = root.nodeName; //結(jié)果為"book"
var att = root.attributes; //得到該點(diǎn)下所有屬性的集合
var str = "";
for (var i=0; i<att.length; i++)
{
str += att.item(i).nodeName+":"+att.item(i).text;
}
Form1.test1.value = str; //只有一個(gè)屬性,所以結(jié)果為“l(fā)evel:1”
var fNode;
var lNode;
var nextSibling;
fNode = root.firstChild; //第一個(gè)子節(jié)點(diǎn)Name
lNode = root.lastChild; //最后一個(gè)子節(jié)點(diǎn) info
nextSibling = fNode.nextSibling; //第一個(gè)子節(jié)點(diǎn)Name的后一個(gè)兄弟節(jié)點(diǎn),即Price
str = fNode.nodeName + ":" + fNode.text; //結(jié)果:"Name:c++"
str = lNode.nodeName + ":" + lNode.text; //結(jié)果為:"info:2"
str = nextSibling.nodeName + ":" + nextSibling.text; //結(jié)果為:"Price:20"
var nodeList;
str = "";
nodeList = xmlDoc.selectNodes("http://info"); //查找元素名為"info"的節(jié)點(diǎn)
for (var j=0; j<nodeList.length; j++) //有兩個(gè)info節(jié)點(diǎn)
{
var infoNode = nodeList.item(j);
var cldNodes = infoNode.childNodes; //info節(jié)點(diǎn)的子節(jié)點(diǎn)集
for (var k=0; k<cldNodes.length; k++)
{
str += cldNodes.item(k).nodeName + ":" + cldNodes.item(k).text + " ";
}
//結(jié)果“k:1 k:2 ”
}
str = "";
var sNode;
sNode = xmlDoc.selectSingleNode("http://info"); //找到第一個(gè)和"info"匹配的
var scldNodes = sNode.childNodes; //info節(jié)點(diǎn)的子節(jié)點(diǎn)集
for (var t=0; t<scldNodes.length; t++)
{
str += scldNodes.item(t).nodeName + ":" + scldNodes.item(t).text + " ";
}
//結(jié)果“k:1”
Form1.test1.value = str;
您可能感興趣的文章:
- Javascript標(biāo)準(zhǔn)DOM Range操作全集
- JavaScript 節(jié)點(diǎn)操作 以及DOMDocument屬性和方法
- Javascript入門(mén)學(xué)習(xí)第七篇 js dom實(shí)例操作
- javascript dom 操作詳解 js加強(qiáng)
- javascript dom操作之cloneNode文本節(jié)點(diǎn)克隆使用技巧
- JS DOM 操作實(shí)現(xiàn)代碼
- JavaScript 高級(jí)篇之DOM文檔,簡(jiǎn)單封裝及調(diào)用、動(dòng)態(tài)添加、刪除樣式(六)
- js和jquery對(duì)dom節(jié)點(diǎn)的操作(創(chuàng)建/追加)
- javascript將DOM節(jié)點(diǎn)添加到文檔的方法實(shí)例分析
- JavaScript利用HTML DOM進(jìn)行文檔操作的方法
相關(guān)文章
javascript 操作cookies及正確使用cookies的屬性
在 JS(JavaScript) 操作cookies比較復(fù)雜,在 ASP 里面我們只需要知道 cookie 的名稱(chēng)、cookie 的值就行了,而 JS 里面,我們面對(duì)的是 cookie 的字符串,你自己編寫(xiě)這個(gè)字符串寫(xiě)入客戶(hù)端,然后自己解析這個(gè)字符串。2009-10-10詳解如何編寫(xiě)一個(gè)Typescript的類(lèi)型聲明文件
我們知道TypeScript根據(jù)類(lèi)型聲明進(jìn)行類(lèi)型檢查,但有些情況可能沒(méi)有類(lèi)型聲明,這個(gè)時(shí)候就需要我們自己寫(xiě)一個(gè),下面小編就來(lái)和大家聊聊如果寫(xiě)一個(gè)Typescript的類(lèi)型聲明文件呢2023-06-06原生js實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)代碼分享
這篇文章主要介紹了原生js實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)功能以及代碼分享,對(duì)此有需要的朋友可以參考學(xué)習(xí)下。2018-02-02js判斷登陸用戶(hù)名及密碼是否為空的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇js判斷登陸用戶(hù)名及密碼是否為空的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05JavaScript關(guān)于提高網(wǎng)站性能的幾點(diǎn)建議(一)
這篇文章主要介紹了JavaScript關(guān)于提高網(wǎng)站性能的幾點(diǎn)建議(一)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07javascript中的self和this用法小結(jié)
本篇文章主要是對(duì)javascript中的self和this用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02JS 兩個(gè)字符串時(shí)間的天數(shù)差計(jì)算
本文為大家介紹下兩個(gè)字符串時(shí)間的天數(shù)差的計(jì)算公式,感興趣的朋友可以參考下2013-08-08