Javascript里使用Dom操作Xml
2024-09-06 12:44:52
供稿:网友
一.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下实现代码:
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时路径不对,会返回“系统未找到指定的对象”的错误
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>”