zoukankan      html  css  js  c++  java
  • Javascript 操作XML简单介绍

    一般从服务端的返回可以得到一个XML对象。
    例如服务器返回的:XMLHttpRequest.ResponseXML
    这里的XMLHttpRequest就是ajax的核心对象。
    在IE下可以这样创建:xmlHttp = new ActiveXObject("Microsoft.XMLHTTP").
    javascript操作XML先创建一个XML DOM对象:var dom = new ActiveXObject("Microsoft.XMLDOM");
    然后dom.loadXML(ResponseXML)就ok了。
    接下来就可以操作xml,获取内容了。
    一些常用的函数如下(一些在网上收集的,一些时平时老大教的):
    Microsoft.XMLDOM 对象常用的属性:
    1、attributes 属性,返回当前节点的属性列表
    2、childNodes 属性,返回当前节点的所有子节点列表
    3、documentElement 属性,返回xml文件的根节点,通过Microsoft.XMLDOM对象名来调用
    4、firstChild 属性、lastChild 属性,返回当前节点的第一个子(最后一个)元素(如果没有子节点是不是返回
    第一个属性?)
    5、nextSibling (previousSibling )属性,下一个兄弟节点。
    6、nodeName 属性,返回节点的标签名字
    7、nodeValue 属性,传回指定节点相关的文字(不是属性,就是*号的这个内容 **)
    8、ownerDocument 属性,根节点
    9、parentNode 属性,传回目前节点的父节点。只能应用在有父节点的节点中。

    例子一:

    <script language="javascript">
    <!--
    if (top == self) 

    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async
    ="false" 
    xmlDoc.load(
    "http://www.cnblogs.com/xml/Message.xml") ;    
    objNodeList
    =xmlDoc.getElementsByTagName("SetJobTypeFirst");
    for (var i=0; i<objNodeList.length; i++
    { alert(objNodeList[
    0].attributes[0].text);}
    }
    top.location.href 
    = "../webMainFrame.aspx";

    -->
        
    </script>

    Message.xml里的定义:

    <?xml version="1.0" encoding="GB2312" ?>
    <Message>
     <SetJobTypeFirst content="Set JobType First in Job Info." />
    </Message>


    例子二:
    function Add()
    {
    var dom = new ActiveXObject("Microsoft.XMLDOM");
    dom.loadXML(ret);
    if (dom.documentElement != null)
    {
    var nodes = dom.documentElement.selectNodes("//SelectItem"); //得到根节点下所有SelectItem节点
    if (nodes != null)
    {
    for(var i=0;i
    一些常用的函数:
    1、AppendChild 方法,加上一个节点当作指定节点最后的子节点。
    2、cloneNode(deep)方法,deep 是一个布尔值。如果为true,此节点会复制以指定节点发展出去的所有节
    点。如果是false,只有指定的节点和它的属性被复制。
    3、createAttribute(name)方法,建立一个指定名称的属性。
    4、createElement 方法,建立一个指定名称的元素。
    5、xmlDocument.createNode(type, name, nameSpaceURI);type 用来确认要被建立的节点型态,name 是一个字符
    串来确认新节点的名称,命名空间的前缀则是选择性的。nameSpaceURI 是一个定义命名空间URI 的字
    符串。如果前缀被包含在名称参数中,此节点会在nameSpaceURI 的内文中以指定的前缀建立。如果不
    包含前缀,指定的命名空间会被视为预设的命名空间。
    6、getElementsByTagName 方法,传回指定名称的元素集合。
    7、haschildnodes 方法,要解释吗?
    8、insertBefore 方法,在指定的节点前插入一个子节点。xmlDocumentNode.insertBefore
    (newChild,refChild);refChild 是参照节点的地址。新子节点被插到参照节点之前。如果refChild 参数没有包含
    在内,新的子节点会被插到子节点列表的末端。
    9、load 方法和loadXML 方法,前这从url,后者从字符串片断。
    10、nodeFromID 方法,传回节点ID 符合指定值的节点。
    11、removeChild 方法和replaceChild(newChild,oldChild),顾名思义
    12、selectNodes和selectSingleNode 方法,传回所有符合提供样式的节点。参数为一包含XSL 样式的字符串。
    以下收集了一些MSDN的例子
    (1)
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    var rootElement=xmlDoc.createElement("memo");
    xmlDoc.appendChild(rootElement);(2) var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    var rootElement=xmlDoc.createElement("memo");
    rootElement.setAttribute("author", "Pat Coleman"); //属性author的值为Pat Coleman
    xmlDoc.appendChild(rootElement);
    (3) var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    var rootElement=xmlDoc.createElement("memo");
    var memoAttribute=xmlDoc.createAttribute("author");
    var memoAttributeText=xmlDoc.createTextNode("Pat Coleman");
    memoAttribute.appendChild(memoAttributeText);
    rootElement.setAttributeNode(memoAttribute);
    xmlDoc.appendChild(rootElement);
    //这个例子和(2)同样效果,但是用不同的方法,这里把attribute也当做一个节点,attribute node的
    子节点只可以是textnode,所以这里要先创建一个textnode在赋给他。
    (4)
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    var rootElement=xmlDoc.createElement("memo"); //创建一个元素
    var memoAttribute=xmlDoc.createAttribute("author"); //创建一个属性
    var memoAttributeText=xmlDoc.createTextNode("Pat Coleman"); //创建一个文本节点
    var toElement=xmlDoc.createElement("to"); //再创建一个元素
    var toElementText=xmlDoc.createTextNode("Carole Poland"); //再创建一个文本节点
    memoAttribute.appendChild(memoAttributeText);
    xmlDoc.appendChild(rootElement);
    rootElement.setAttributeNode(memoAttribute);
    rootElement.appendChild(toElement);
    toElement.appendChild(toElementText);


    属性:
    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.
    firstChild
    Contains the first child of the node. Read-only.
    lastChild
    Returns the last child node. Read-only.
    name
    Contains the attribute name. Read-only.
    namespaceURI
    *Returns the Uniform Resource Identifier (URI) for the namespace. Read-only.
    nextSibling
    Contains the next sibling of this node in the parent's child list. Read-only.
    nodeName
    Contains the qualified name of the element, attribute, or entity reference, or a fixed string for 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 the node 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.
    ownerDocument
    Returns the root of the document that contains the node. Read-only.
    parentNode
    Contains the parent node. Read-only.
    parsed
    *Indicates the parsed status of the node and child nodes. Read-only.
    prefix
    *Returns the namespace prefix. Read-only.
    previousSibling
    Contains the previous sibling of this node in the parent's child list. Read-only.
    specified
    Indicates whether the node (usually an attribute) is explicitly specified or derived from a default value in the document type definition (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.
    value
    Contains the attribute value. Read/write.
    xml
    Contains the XML representation of the node and all its descendants. Read-only.
    方法:
    appendChild
    Appends new child node as the last child of this node.
    cloneNode
    Clones a new node.
    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.
    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.
    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 XSL Transformations (XSLT) style sheet and returns the resulting transformation.
    transformNodeToObject
    Processes this node and its children using the supplied XSL Transformations (XSLT) style sheet and returns the resulting transformation in the supplied object. 

  • 相关阅读:
    禅道 之 项目开发必备
    Cmd 命令大全
    Php 性能参数优化 及 Iptables 防火墙限制用户访问平率
    Nginx 性能参数优化
    Mysql 性能调优参数
    Postfix的工作原理
    python三次输入错误验证登录
    python shopping incomplete code
    MySQL + Atlas --- 部署读写分离
    网站流量分析项目day03
  • 原文地址:https://www.cnblogs.com/zzh/p/1146774.html
Copyright © 2011-2022 走看看