Javascript里使用Dom操作Xml
2024-09-06 12:43:43
供稿:网友
看了一天的XML资料,感觉CSDN上这篇讲的挺细致的。即有Dot Net写入XML文件的示例,又有JS读取的示例,值得一看。(Source:http://blog.csdn.net/flypigluo)
一.本笔记使用的Xml文件
二.IXMLDOMDocument/DOMDocument简介
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下实现代码:
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简介
2.1 属性
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error
返回解析错误时的一个对象。
重要的有parseError.errorCode,parseError.reason
如果load时路径不对,会返回“系统未找到指定的对象”的错误
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.