Javascript里使用Dom操作Xml笔记之常用函数[转]

news/2025/2/25 8:05:38

一.本笔记使用的Xml文件

 

<?xml version="1.0"?>

<book level="1"> 
      <Name>c++</Name>

 

     <rice>20</Price>

      <info>
            <k>1</k>

       </info>

      <info>

          <k>2</k> 
       </info>

 

</book>

 

二.IXMLDOMDocument/DOMDocument简介

2.1 属性

211  parseError

      Returns an IXMLDOMParseError object that contains information about the last parsing error返回解析错误时的一个对象。重要的
    有
parseError.errorCode,parseError.reason如果load时路径不对,会返回“系统未找到指定的对象”的错误
asd

212  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>

214  text

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

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

<price>20</price>

text20

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

215  attributes

Contains the list of attributes for this node

返回属性的集合。

216  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

该节点名称

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

217  documentElement

Contains the root element of the document

xml的根节点

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

218  nextSibling

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

下一个兄弟节点,只读

219  childNodes

Contains a node list containing the child nodes

所有的子节点。

2110  firstChild

Contains the first child of the node

第一个子节点

2111  lastChild

Returns the last child node

最后一个子节点

 

 

2.2 方法

221  loadXML

Loads an XML document using the supplied string

222 load

Loads an XML document from the specified locati

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

223  selectSingleNode

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

返回第一个匹配的项

224  selectNodes

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

符合条件的所有项。

225  getElementsByTagName

Returns a collection of elements that have the specified name

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

226  hasChildNodes

Provides a fast way to determine whether a node has children

判断是否含有子节点

返回值为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;   //根节点
Form1.test1.value = root.xml;
/*结果如下:
<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; //结果为"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; //只有一个属性,所以结果为“level1
var fNode;
var lNode;
var nextSibling;
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"
var nodeList;
str = "";
nodeList = xmlDoc.selectNodes("//info"); //查找元素名为"info"的节点
for (var j=0; j<nodeList.length; j++) //有两个info节点
{
      var infoNode = nodeList.item(j);
      var cldNodes = infoNode.childNodes; //info节点的子节点集
      for (var k=0; k<cldNodes.length; k++)
      {
             str += cldNodes.item(k).nodeName + ":" + cldNodes.item(k).text + " ";
       }
      //结果“k:1 k:2
}

str = "";
var sNode;
sNode = xmlDoc.selectSingleNode("//info"); //找到第一个和"info"匹配的
var scldNodes = sNode.childNodes; //info节点的子节点集
for (var t=0; t<scldNodes.length; t++)
{
      str += scldNodes.item(t).nodeName + ":" + scldNodes.item(t).text + " ";
}
//结果“k:1
Form1.test1.value = str;

转载于:https://www.cnblogs.com/visoeclipse/archive/2009/08/27/1554898.html


http://www.niftyadmin.cn/n/674413.html

相关文章

.net reflector 6.6安装注册

1.官网下载安装包含.net reflector的集合包&#xff0c;单独下载的版本无法注册 .NET Developer Bundle&#xff08;当前版本1.6.1.3&#xff09; 2.下载danny su 的 RedGate Licensing http://rapidshare.com/files/396596167/RedGate.Licensing.exe 3.安装原版&#xff0c;选…

菜鸟学习笔记-linux shell 下不拷贝和tab使用带来所执行程序不认的情况

&#xff08;1&#xff09; 银河麒麟下&#xff0c;将文本中的命令拷贝到shell 命令行下出现异常错误 &#xff08;2&#xff09;ubuntu 18.04 下自己的测试demo 在输入文件路径时候&#xff0c;错用tab按键&#xff0c;再退格&#xff0c;程序出现异常&#xff0c;可以执行&a…

ASP.NET 2.0轻松搞定统计图表(二)

前台代码&#xff1a;<% Page Language"C#" AutoEventWireup"true" CodeFile"OWCdrawing.aspx.cs"Inherits"OWCdrawing" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.o…

iperf2.0和iperf3比较以及影响udp丢包率的参数

文章背景&#xff1a; 最近在做40G交换板的TCP带宽和UDP带宽、丢包率、抖动测试&#xff1b; 发现iperf与iperf3版本不同对测试结果有较大影响&#xff1b;通过自己试验与网络查找资料有结论如下。 第一部分&#xff1a;iperf与iperf3比较 1&#xff09;iperf3较iperf测试U…

【图论】【poj 3026】Borg Maze

问题 S在迷宫中找A&#xff0c;找到A之后就把它同化&#xff0c;也会帮着S找剩下的A。。就是这样。。给你的是一个字符组成的图…… 分析 正常人会想到搜索……当然&#xff0c;但是只是搜索很难实现&#xff0c;仔细读题&#xff0c;我们发现&#xff0c;其实质是在求最小生成…

30天制作操作系统,第一天!

第一天介绍的内容是用二进制编辑器手敲十六进制数&#xff0c;生成img镜像文件&#xff0c;借助模拟器启动“自己的操作系统”&#xff0c; 打印“hello world”&#xff0c; 即下图&#xff01; 然而30天制作操作系统的作者&#xff0c;由于成书时间过于久远&#xff0c;并…

【老孙随笔】解除项目经理焦虑痛苦的良药——谦虚

副标题——谦虚也有益于自身的精神健康&#xff08; 作者&#xff1a;孙继滨 &#xff09; 步入正题前&#xff0c;请允许老孙先给大家重新演绎一个故事 —— 《白雪公主》******************************* 【故事简介】 故事讲的一个美丽的女人的故事。 当然&#xff0c;主人…