Javascript裡使用Dom操作Xml筆記之常用函數

來源:互聯網
上載者:User
一.本筆記使用的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 屬性 2.1.1  parseError

      Returns an IXMLDOMParseError object that contains information about the last parsing error返回解析錯誤時的一個對象。重要的
    有parseError.errorCode,parseError.reason如果load時路徑不對,會返回“系統未找到指定的對象”的錯誤
asd

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

2.1.4  text

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

該點及下面派生的所有點的全部節點值,可讀可寫

<price>20</price>

則text為20

"Name"節點的text為"c++"

2.1.5  attributes

Contains the list of attributes for this node

返回屬性的集合。

2.1.6  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"

2.1.7  documentElement

Contains the root element of the document

xml的根節點

上面的xml的根節點為"book"

2.1.8  nextSibling

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

下一個兄弟節點,唯讀

2.1.9  childNodes

Contains a node list containing the child nodes

所有的子節點。

2.1.10  firstChild

Contains the first child of the node

第一個子節點

2.1.11  lastChild

Returns the last child node

最後一個子節點

 

 

2.2 方法 2.2.1  loadXML

Loads an XML document using the supplied string

2.2.2 load

Loads an XML document from the specified locati

參數的路徑為伺服器端的,是相對路徑

2.2.3  selectSingleNode

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

返回第一個匹配的項

2.2.4  selectNodes

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

合格所有項。

2.2.5  getElementsByTagName

Returns a collection of elements that have the specified name

返回與元素名匹配的一個node的集合

2.2.6  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; //只有一個屬性,所以結果為“level:1”
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;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.