JavaScript操作Xml

如果你做Web开发,那就难免要与JavaScript打交道。而JavaScript作为一种浏览器脚本,以其强大的功能及方便的操作,已经赢得了全部浏览器的支持。在前面我介绍了如何使用Sql操作Xml使用C#操作Xml,这里我就简单的介绍一下使用JavaScript来操作Xml数据。

     学习任何知识的最佳途径莫过于实践。这里我们从一个简单的例子出发,来讲解如何使用JavaScript来操作Xml格式数据的。由于我的水平有限,不正确或者不合理的地方还请指正。

     1、创建文档。

     大家都知道,Xml是一种基于对象的语言,也就是说,在JavaScript中,很多东西也都是面向对象的。可以使用new关键字创建一个对象。当然,创建一个Xml文档也不例外。var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");

     2、添加节点。

     其实,使用JavaScript操作Xml,和使用C#操作Xml有着几乎相同的方法。我们可以使用C#中一样的方法appendChild()方法来实现添加XML节点。说到这里你该问如何创建Xml节点了,其实还是跟C#相同的方法 var root = xmlDoc.createElement("root"); 将这个root节点添加到xmlDoc这个文档中就很简单了: xmlDoc.appendChild(root); 现在,xmlDoc中就有了一个根节点了。需要注意的是,一个Xml文档只能有一个根节点。其它的节点个数不限。

     3、得到节点。

     查询节点的方法就很多了。可以使用一个一个节点遍历,也可以使用selectSingleNode()方法或者selectNodes()方法。而得到一个文档根节点的方法则是通过属性documentElement来得到。那么得到xmlDoc的根节点就是 var rootNode = xmlDoc.documentElement;现在我们向root节点中添加一个book节点:var book = xmlDoc.createElement("book"); root.appendChild(bookNode); 那么得到book节点就是:var bookNode = root.selectSingleNode("book");或者var bookNode = root.selectNodes("book")[0]; 或者var bookNode = root.firstNode;你可以使用任何一种方法来得到自己想要的节点。

     4、删除节点。

     其实这个功能使用量是非常少的。你可以使用removeChild()方法来操作。这里不再详细说明。

     5、未提到的内容。

     对Xml的操作还远不止这些。还有对属性的增删改查的操作, 对节点值的操作。详细的说明请参照下面的这张表,这个表格来源与MSDN:


async


Specifies if asynchronous download is permitted. Read/write.


attributes


Contains the list of attributes for this node. Read-only.


baseName


Returns the base name for the name qualified with the namespace. Read-only.


childNodes


Contains a node list containing the children nodes. Read-only.


dataType


Specifies the data type for this node. Read/write.


definition


Returns the definition of the node in the document type definition (DTD) or schema. Read-only.


doctype


Contains the document type node that specifies the DTD for this document. Read-only.


documentElement


Contains the root element of the document. Read/write.


firstChild


Contains the first child of this node. Read-only.


implementation


Contains the IXMLDOMImplementation object for the document. Read-only.


lastChild


Returns the last child node. Read-only.


namespaceURI


Returns the Uniform Resource Identifier (URI) for the namespace. Read-only.


nextSibling


Contains the next sibling of the node in the parent's child list. Read-only.


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.


nodeType


Specifies the XML Document Object Model (DOM) node type, which determines valid values and whether the node can have child nodes. Read-only.


nodeTypedValue


Contains this node's value expressed in its defined data type. Read/write.


nodeTypeString


Returns the node type in string form. Read-only.


nodeValue


Contains the text associated with the node. Read/write.


ondataavailable


Specifies the event handler for the ondataavailable event. Write-only.


onreadystatechange


Specifies the event handler to be called when the readyState property changes. Write-only.


ontransformnode


Specifies the event handler for the ontransformnode event. Write-only.


ownerDocument


Returns the root of the document that contains this node. Read-only.


parentNode


Contains the parent node. Read-only.


parsed


Indicates the parsed status of the node and child nodes. Read-only.


parseError


Returns an IXMLDOMParseError object that contains information about the last parsing error. Read-only.


prefix


Returns the namespace prefix. Read-only.


preserveWhiteSpace


Specifies the default white space handling. Read/write.


previousSibling


Contains the previous sibling of the node in the parent's child list. Read-only.


readyState


Indicates the current state of the XML document. Read-only.


resolveExternals


Indicates whether external definitions (resolvable namespaces, DTD external subsets, and external entity references) are to be resolved at parse time, independent of validation. Read/write.


specified


Indicates whether the node (usually an attribute) is explicitly specified or derived from a default value in the DTD or schema. Read-only.


text


Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write.


url


Returns the URL for the last loaded XML document. Read-only.


validateOnParse


Indicates whether the parser should validate this document. Read/write.


xml


Contains the XML representation of the node and all its descendants. Read-only.


abort


Aborts an asynchronous download in progress.


appendChild


Appends a new child as the last child of this node.


cloneNode


Clones a new node.


createAttribute


Creates a new attribute with the specified name.


createCDATASection


Creates a CDATA section node that contains the supplied data.


createComment


Creates a comment node that contains the supplied data.


createDocumentFragment


Creates an empty IXMLDOMDocumentFragment object.


createElement


Creates an element node using the specified name.


createEntityReference


Creates a new EntityReference object.


createNode


Creates a node using the supplied type, name, and namespace.


createProcessingInstruction


Creates a processing instruction node that contains the supplied target and data.


createTextNode


Creates a text node that contains the supplied data.


getElementsByTagName


Returns a collection of elements that have the specified name.


hasChildNodes


Provides a fast way to determine whether a node has children.


insertBefore


Inserts a child node to the left of the specified node or at the end of the list.


load


Loads an XML document from the specified location.


loadXML


Loads an XML document using the supplied string.


nodeFromID


Returns the node that matches the ID attribute.


removeChild


Removes the specified child node from the list of children and returns it.


replaceChild


Replaces the specified old child node with the supplied new child node.


save


Saves an XML document to the specified location.


selectNodes


Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList.


selectSingleNode


Applies the specified pattern-matching operation to this node's context and returns the first matching node.


transformNode


Processes this node and its children using the supplied XSLT style sheet and returns the resulting transformation.


transformNodeToObject


Processes this node and its children using the supplied XSLT style sheet and returns the resulting transformation in the supplied object.

 

声明

作者:齐飞

Email:youring2@gmail.com
欢迎转载,但请保留本段信息!

 

如果认为此文对您有帮助,别忘了支持一下哦!

作者:齐飞

来源:http://youring2.cnblogs.com/

声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权,贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。

转载:http://www.cnblogs.com/youring2/archive/2008/12/16/1356198.html

时间: 2024-08-06 08:54:00

JavaScript操作Xml的相关文章

用javascript操作xml

javascript|xml <script language="JavaScript"><!--var doc = new ActiveXObject("Msxml2.DOMDocument"); //ie5.5+,CreateObject("Microsoft.XMLDOM") //加载文档//doc.load("b.xml"); //创建文件头var p = doc.createProcessingIn

JavaScript操作XML文件之XML读取方法

 本文实例讲述了JavaScript操作XML文件之XML读取方法.分享给大家供大家参考.具体分析如下: 假设我们现在要读取下面的 info.xml 文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?xml version="1.0" encoding="gb2312"?> <root> <data id="1"> <name

JavaScript操作XML/HTML比较常用的对象属性集锦_javascript技巧

节点对象属性 childNodes-返回节点到子节点的节点列表 firstChild-返回节点的首个子节点. lastChild-返回节点的最后一个子节点. nextSibling-返回节点之后紧跟的同级节点. nodeName-返回节点的名字,根据其类型. nodeType-返回节点的类型. nodeValue-设置或返回节点的值,根据其类型. ownerDocument-返回节点的根元素(document对象). parentNode-返回节点的父节点. previousSibling-返回

JavaScript操作XML 使用百度RSS作为新闻源示例_javascript技巧

js操作xml源,作为页面的动态新闻 参考JS源码如下(存为rss.js文件): 复制代码 代码如下: var main = document.getElementById("content").getElementsByTagName("DIV"); /* * 当前目录下面有一个名为xml的子文件夹,下面引用的源保存在目录下. * 下面每一行的冒号前面是文件名,后面是xml源地址(可以从源址下载得到xml文件,下载后保存为对应文件名) * 可以在下面的地址上单击右

JavaScript操作XML实例代码(获取新闻标题并分页,并分页)_javascript技巧

具体内容我没有做测试.仅供参考 代码 复制代码 代码如下: <?xml version="1.0" encoding="gb2312"?> <NEWS> <New id="1" name="测试新闻1" time="2010-2-18"> <NBody>新闻测试1新闻测试1</NBody> </New> <New id="

javascript操作xml(增删改查)例子代码hta版_hta

包括了stu.hta(是HTML应用程序);      stu.xml 注意下面的HTML代码必须保存为后缀名为hta否则当对XML文件进行操作(增删改)的时候就会提示没有权限!! 文件stu.hta代码如下: 复制代码 代码如下: <html> <head> <title> 数据岛的显示 </title> <style type="text/css"> #findPanel { position:absolute; widt

JavaScript将XML转成JSON的方法

 这篇文章主要介绍了JavaScript将XML转成JSON的方法,实例分析了javascript操作XML文件及格式转化的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了JavaScript将XML转成JSON的方法.分享给大家供大家参考.具体方法如下: 1. JavaScript代码如下: 代码如下: // Changes XML to JSON function xmlToJson(xml) { // Create the return object var obj

javascript调用XML制作连动下拉框

传统的HTML页面中连动下拉框采用了两种方法:1)直接将下拉框中的内容hardcode于html的javascript中,调用javascript函数循环写入下拉框中.这种方法不适用于下拉框内容经常改变的情况.因为数据源和javascript程序写死在同一页面.   <html><head><title>List</title><meta http-equiv="Content-Type" content="text/ht

Javascript 调用XML制作连动下拉框

javascript|xml|下拉 传统的HTML页面中连动下拉框采用了两种方法:1)直接将下拉框中的内容hardcode于html的javascript中,调用javascript函数循环写入下拉框中.这种方法不适用于下拉框内容经常改变的情况.因为数据源和javascript程序写死在同一页面.   <html><head><title>List</title><meta http-equiv="Content-Type" con