Javascript里使用Dom操作Xml
更新時間:2006年09月20日 00:00:00 作者:
一.Xml文件
二.IXMLDOMDocument/DOMDocument簡介
2.1 屬性
2.1.1 parseError
2.1.2 async.
2.1.3 xml
2.1.4 text 3
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下實現(xiàn)代碼:
string str = Server.MapPath("test1.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(str,null);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.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簡介
2.1 屬性
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error
返回解析錯誤時的一個對象。
重要的有parseError.errorCode,parseError.reason
如果load時路徑不對,會返回“系統(tǒng)未找到指定的對象”的錯誤
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.
該點及下面派生的所有點的全部信息,只讀如果要求book點的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
該點及下面派生的所有點的全部節(jié)點值,可讀可寫
<price>20</price>
則text為20
"Name"節(jié)點的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é)點名稱
"Name"節(jié)點的nodeName為"Name","book"節(jié)點的nodeName為"book"
2.1.7 documentElement
Contains the root element of the document
xml的根節(jié)點
上面的xml的根節(jié)點為"book"
2.1.8 nextSibling
Contains the next sibling of the node in the parent's child list. Read-only.
下一個兄弟節(jié)點,只讀
2.1.9 childNodes
Contains a node list containing the child nodes
所有的子節(jié)點。
2.1.10 firstChild
Contains the first child of the node
第一個子節(jié)點
2.1.11 lastChild
Returns the last child node
最后一個子節(jié)點
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ù)器端的,是相對路徑
2.2.3 selectSingleNode
Applies the specified pattern-matching operation to this node's context and returns the first matching node
返回第一個匹配的項
2.2.4 selectNodes
Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList
符合條件的所有項。
2.2.5 getElementsByTagName
Returns a collection of elements that have the specified name
返回與元素名匹配的一個node的集合
2.2.6 hasChildNodes
Provides a fast way to determine whether a node has children
判斷是否含有子節(jié)點
返回值為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é)點
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; //得到該點下所有屬性的集合
var str = "";
for (var i=0; i<att.length; i++)
{
str += att.item(i).nodeName+":"+att.item(i).text;
}
Form1.test1.value = str; //只有一個屬性,所以結(jié)果為“l(fā)evel:1”
var fNode;
var lNode;
var nextSibling;
fNode = root.firstChild; //第一個子節(jié)點Name
lNode = root.lastChild; //最后一個子節(jié)點 info
nextSibling = fNode.nextSibling; //第一個子節(jié)點Name的后一個兄弟節(jié)點,即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é)點
for (var j=0; j<nodeList.length; j++) //有兩個info節(jié)點
{
var infoNode = nodeList.item(j);
var cldNodes = infoNode.childNodes; //info節(jié)點的子節(jié)點集
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"); //找到第一個和"info"匹配的
var scldNodes = sNode.childNodes; //info節(jié)點的子節(jié)點集
for (var t=0; t<scldNodes.length; t++)
{
str += scldNodes.item(t).nodeName + ":" + scldNodes.item(t).text + " ";
}
//結(jié)果“k:1”
Form1.test1.value = str;
二.IXMLDOMDocument/DOMDocument簡介
2.1 屬性
2.1.1 parseError
2.1.2 async.
2.1.3 xml
2.1.4 text 3
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下實現(xiàn)代碼:
string str = Server.MapPath("test1.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(str,null);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.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簡介
2.1 屬性
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error
返回解析錯誤時的一個對象。
重要的有parseError.errorCode,parseError.reason
如果load時路徑不對,會返回“系統(tǒng)未找到指定的對象”的錯誤
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.
該點及下面派生的所有點的全部信息,只讀如果要求book點的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
該點及下面派生的所有點的全部節(jié)點值,可讀可寫
<price>20</price>
則text為20
"Name"節(jié)點的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é)點名稱
"Name"節(jié)點的nodeName為"Name","book"節(jié)點的nodeName為"book"
2.1.7 documentElement
Contains the root element of the document
xml的根節(jié)點
上面的xml的根節(jié)點為"book"
2.1.8 nextSibling
Contains the next sibling of the node in the parent's child list. Read-only.
下一個兄弟節(jié)點,只讀
2.1.9 childNodes
Contains a node list containing the child nodes
所有的子節(jié)點。
2.1.10 firstChild
Contains the first child of the node
第一個子節(jié)點
2.1.11 lastChild
Returns the last child node
最后一個子節(jié)點
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ù)器端的,是相對路徑
2.2.3 selectSingleNode
Applies the specified pattern-matching operation to this node's context and returns the first matching node
返回第一個匹配的項
2.2.4 selectNodes
Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList
符合條件的所有項。
2.2.5 getElementsByTagName
Returns a collection of elements that have the specified name
返回與元素名匹配的一個node的集合
2.2.6 hasChildNodes
Provides a fast way to determine whether a node has children
判斷是否含有子節(jié)點
返回值為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é)點
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; //得到該點下所有屬性的集合
var str = "";
for (var i=0; i<att.length; i++)
{
str += att.item(i).nodeName+":"+att.item(i).text;
}
Form1.test1.value = str; //只有一個屬性,所以結(jié)果為“l(fā)evel:1”
var fNode;
var lNode;
var nextSibling;
fNode = root.firstChild; //第一個子節(jié)點Name
lNode = root.lastChild; //最后一個子節(jié)點 info
nextSibling = fNode.nextSibling; //第一個子節(jié)點Name的后一個兄弟節(jié)點,即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é)點
for (var j=0; j<nodeList.length; j++) //有兩個info節(jié)點
{
var infoNode = nodeList.item(j);
var cldNodes = infoNode.childNodes; //info節(jié)點的子節(jié)點集
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"); //找到第一個和"info"匹配的
var scldNodes = sNode.childNodes; //info節(jié)點的子節(jié)點集
for (var t=0; t<scldNodes.length; t++)
{
str += scldNodes.item(t).nodeName + ":" + scldNodes.item(t).text + " ";
}
//結(jié)果“k:1”
Form1.test1.value = str;
您可能感興趣的文章:
- js使用DOM操作實現(xiàn)簡單留言板的方法
- javascript拓展DOM操作 prependChild insertAfert
- javascript DOM操作之動態(tài)刪除TABLE多行
- javascript中HTMLDOM操作詳解
- 簡單實現(xiàn)JS對dom操作封裝
- JavaScript DOM操作表格及樣式
- JavaScript基礎(chǔ)語法、dom操作樹及document對象
- JavaScript基于Dom操作實現(xiàn)查找、修改HTML元素的內(nèi)容及屬性的方法
- JavaScript中 DOM操作方法小結(jié)
- JavaScript學習筆記之DOM操作實例分析
相關(guān)文章
H5如何實現(xiàn)喚起APP及調(diào)試bug解決
這篇文章主要為大家介紹了H5如何實現(xiàn)喚起APP及調(diào)試bug解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
jQuery 表單驗證插件formValidation實現(xiàn)個性化錯誤提示
這里介紹另外一個表單驗證插件formValidation,這個插件與前面兩個插件的最大區(qū)別在于它實現(xiàn)了個性化的錯誤提示信息,顯示在表單元素右上角類似于提示條2009-06-06
Javascript DOM的簡介,節(jié)點和獲取元素詳解
下面小編就為大家分享一篇詳談DOM的簡介,節(jié)點和獲取元素,具有非常好的參考價值,一起跟隨小編過來看看吧,希望對大家有所幫助2021-11-11
JS中使用gulp實現(xiàn)壓縮文件及瀏覽器熱加載功能
這篇文章主要介紹了JS中使用gulp實現(xiàn)壓縮文件及瀏覽器熱加載功能,需要的朋友可以參考下2017-07-07

