Javascript里使用Dom操作Xml
Javascript里使用Dom操作Xml
发布时间:2016-12-30 来源:查字典编辑
摘要:看了一天的XML资料,感觉CSDN上这篇讲的挺细致的。即有DotNet写入XML文件的示例,又有JS读取的示例,值得一看。(Source:h...

看了一天的XML资料,感觉CSDN上这篇讲的挺细致的。即有DotNet写入XML文件的示例,又有JS读取的示例,值得一看。(Source:http://blog.csdn.net/flypigluo)

一.本笔记使用的Xml文件

二.IXMLDOMDocument/DOMDocument简介

2.1属性

2.1.1parseError

2.1.2async.

2.1.3xml

2.1.4text3

2.1.5attributes

2.1.6nodeName

2.1.7documentElement

2.1.8nextSibling

2.1.9childNodes

2.1.10firstChild

2.1.11lashChild

2.2方法

2.2.1loadXML

2.2.2load

2.2.3selectSingleNode

2.2.4selectNodes

2.2.5getElementsByTagName

2.2.6hasChildNodes

三.例子

一.本笔记使用的Xml文件

<?xmlversion="1.0"?>

<booklevel="1"><Name>c++</Name>

<Price>20</Price>

<info>

<k>1</k>

</info>

<info>

<k>2</k>

</info>

</book>

在asp.net下实现代码:

stringstr=Server.MapPath("test1.xml");

XmlTextWriterxmlWriter=newXmlTextWriter(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简介

2.1属性

2.1.1parseError

ReturnsanIXMLDOMParseErrorobjectthatcontainsinformationaboutthelastparsingerror

返回解析错误时的一个对象。

重要的有parseError.errorCode,parseError.reason

如果load时路径不对,会返回“系统未找到指定的对象”的错误

2.1.2async

Specifieswhetherasynchronousdownloadispermitted

是否允许异步下载,布尔值

2.1.3xml

ContainstheXMLrepresentationofthenodeandallitsdescendants.Read-only.

该点及下面派生的所有点的全部信息,只读如果要求book点的xml,返回“<booklevel="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.4text

Representsthetextcontentofthenodeortheconcatenatedtextrepresentingthenodeanditsdescendants.Read/write

该点及下面派生的所有点的全部节点值,可读可写

<price>20</price>

则text为20

"Name"节点的text为"c++"

2.1.5attributes

Containsthelistofattributesforthisnode

返回属性的集合。

2.1.6nodeName

Returnsthequalifiednameforattribute,documenttype,element,entity,ornotationnodes.Returnsafixedstringforall

othernodetypes.Read-only

该节点名称

"Name"节点的nodeName为"Name","book"节点的nodeName为"book"

2.1.7documentElement

Containstherootelementofthedocument

xml的根节点

上面的xml的根节点为"book"

2.1.8nextSibling

Containsthenextsiblingofthenodeintheparent'schildlist.Read-only.

下一个兄弟节点,只读

2.1.9childNodes

Containsanodelistcontainingthechildnodes

所有的子节点。

2.1.10firstChild

Containsthefirstchildofthenode

第一个子节点

2.1.11lastChild

Returnsthelastchildnode

最后一个子节点

2.2方法

2.2.1loadXML

LoadsanXMLdocumentusingthesuppliedstring

2.2.2load

LoadsanXMLdocumentfromthespecifiedlocati

参数的路径为服务器端的,是相对路径

2.2.3selectSingleNode

Appliesthespecifiedpattern-matchingoperationtothisnode'scontextandreturnsthefirstmatchingnode

返回第一个匹配的项

2.2.4selectNodes

Appliesthespecifiedpattern-matchingoperationtothisnode'scontextandreturnsthelistofmatchingnodesasIXMLDOMNodeList

符合条件的所有项。

2.2.5getElementsByTagName

Returnsacollectionofelementsthathavethespecifiedname

返回与元素名匹配的一个node的集合

2.2.6hasChildNodes

Providesafastwaytodeterminewhetheranodehaschildren

判断是否含有子节点

返回值为bool值

三.例子

varxmlDoc=newActiveXObject("Msxml2.DOMDocument.3.0");

xmlDoc.async=false;

xmlDoc.load("testtest1.xml");

if(xmlDoc.parseError.errorCode!=0)

{

varerror=xmlDoc.parseError;

alert(error.reason)

return;

}

varroot=xmlDoc.documentElement;//根节点

Form1.test1.value=root.xml;

/*结果如下:

<booklevel="1"><Name>c++</Name><Price>20</Price><info><k>1</k></info><info><k>2</k></info></book>*/

Form1.test1.value=root.nodeName;//结果为"book"

varatt=root.attributes;//得到该点下所有属性的集合

varstr="";

for(vari=0;i<att.length;i++)

{

str+=att.item(i).nodeName+":"+att.item(i).text;

}

Form1.test1.value=str;//只有一个属性,所以结果为“level:1”

varfNode;

varlNode;

varnextSibling;

fNode=root.firstChild;//第一个子节点Name

lNode=root.lastChild;//最后一个子节点info

nextSibling=fNode.nextSibling;//第一个子节点Name的后一个兄弟节点,即Price

str=fNode.nodeName+":"+fNode.text;//结果:"Name:c++"

str=lNode.nodeName+":"+lNode.text;//结果为:"info:2"

str=nextSibling.nodeName+":"+nextSibling.text;//结果为:"Price:20"

varnodeList;

str="";

nodeList=xmlDoc.selectNodes("//info");//查找元素名为"info"的节点

for(varj=0;j<nodeList.length;j++)//有两个info节点

{

varinfoNode=nodeList.item(j);

varcldNodes=infoNode.childNodes;//info节点的子节点集

for(vark=0;k<cldNodes.length;k++)

{

str+=cldNodes.item(k).nodeName+":"+cldNodes.item(k).text+"";

}

//结果“k:1k:2”

}

str="";

varsNode;

sNode=xmlDoc.selectSingleNode("//info");//找到第一个和"info"匹配的

varscldNodes=sNode.childNodes;//info节点的子节点集

for(vart=0;t<scldNodes.length;t++)

{

str+=scldNodes.item(t).nodeName+":"+scldNodes.item(t).text+"";

}

//结果“k:1”

Form1.test1.value=str;

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类