1. 程式人生 > >JS總結--(XML和Xpath) .

JS總結--(XML和Xpath) .

五、解析xml檔案和字串

ie中: xmlDoc = new ActionXObject("Microsoft.XMLDOM");

          xmlDoc.async = "false";

          xmlDoc.load("books.xml"); //xmlDoc.loadXml(txt);

ff中:xmlDoc = document.implementation.createDocument("","",null);

        xmlDoc.async = "false";      (關閉非同步載入,確保在文件完整載入之前,解析器不會繼續執行指令碼)。

        xmlDoc.load("books.xml");

        //parser =  new DOMParser();

        //xmlDoc = perser.parseFromString(txt,"text/xml");

六、

1、獲取: nodeValue 獲取節點的文字值。

                getAttribute() 返回屬性的值。

                getAttributeNode() 返回屬性的節點。

     eg:  xmlDoc.getElementsByTagname("title")[0].getAttribute("lang");

            xmlDoc.getElementsByTagname("title")[0].getAttributeNode("lang").nodeValue;

2、改變: setAttribute()

3、刪除: removeChild,removeAttribute()

4、替換: replaceChild(), 通過nodaValue替換。

5、建立: eg:    new = xmlDoc.createElment("edition");

                          xmlDoc.getElementsByTagname("book").appendChild(new);

     createAttribute()

               eg:   newatt = xmlDoc.creatAttribute("edition");

                       newatt.nodeValue = "first";

                       xmlDoc.getAttributeByTagname("title").setAttributeNode(newatt);

     setAttribute()

eg:   xmlDoc.getElementsByTagname("title").setAttribute("edition","first");

     creatTextNode()

               eg:   newel = xmlDoc.creatElement("edition");

                       newtext = xmlDoc.createTextNode("first");

                       newel.appendChild(newtext);

                       xmlDoc.getElementsByTagname("book")[0].appendChild(newel);

     creatCDATASection()

     creatComment()

6、新增節點: appendChild()

     插入節點: insertBefore()

               eg:   newnode = xmlDoc.creatElement("book");

                       xmlDoc.documentElement.insertBefore(newnode,xmlDoc.getElementsByTagname("book")[3]);

     x.insertBefore(newnode,null) 和x.appendChild(newNode) 可向x追加一個子節點。

    insertData()  將資料插入到已有的文字節點中。

              eg:    x = xmlDoc.getElementsByTagname("title")[0].childNodes[0];

                       x.insertData(0,"hello");

    cloneNode()

七、Xpath

表示式 描述
nodename 選取此節點的所有子節點
/ 從根節點選取
// 從匹配選擇的當前節點選擇文件中的節點,而不考慮它們的位置
. 選取當前節點
.. 選取當前節點的父節點
@ 選取屬性

例項

在下面的表格中,我們已列出了一些路徑表示式以及表示式的結果:

路徑表示式 結果
bookstore 選取 bookstore 元素的所有子節點
/bookstore

選取根元素 bookstore

註釋:假如路徑起始於正斜槓( / ),則此路徑始終代表到某元素的絕對路徑!

bookstore/book 選取所有屬於 bookstore 的子元素的 book 元素。
//book 選取所有 book 子元素,而不管它們在文件中的位置。
bookstore//book 選擇所有屬於 bookstore 元素的後代的 book 元素,而不管它們位於 bookstore 之下的什麼位置。
//@lang 選取所有名為 lang 的屬性。

謂語(Predicates)

謂語用來查詢某個特定的節點或者包含某個指定的值的節點。

謂語被嵌在方括號中。

例項

在下面的表格中,我們列出了帶有謂語的一些路徑表示式,以及表示式的結果:

路徑表示式 結果
/bookstore/book[1] 選取屬於 bookstore 子元素的第一個 book 元素。
/bookstore/book[last()] 選取屬於 bookstore 子元素的最後一個 book 元素。
/bookstore/book[last()-1] 選取屬於 bookstore 子元素的倒數第二個 book 元素。
/bookstore/book[position()<3] 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。
//title[@lang] 選取所有擁有名為 lang 的屬性的 title 元素。
//title[@lang='eng'] 選取所有 title 元素,且這些元素擁有值為 eng 的 lang 屬性。
/bookstore/book[price>35.00] 選取所有 bookstore 元素的 book 元素,且其中的 price 元素的值須大於 35.00。
/bookstore/book[price>35.00]/title 選取所有 bookstore 元素中的 book 元素的 title 元素,且其中的 price 元素的值須大於 35.00。

選取未知節點

XPath 萬用字元可用來選取未知的 XML 元素。

萬用字元 描述
* 匹配任何元素節點
@* 匹配任何屬性節點
node() 匹配任何型別的節點

例項

在下面的表格中,我們列出了一些路徑表示式,以及這些表示式的結果:

路徑表示式 結果
/bookstore/* 選取 bookstore 元素的所有子節點
//* 選取文件中的所有元素
//title[@*] 選取所有帶有屬性的 title 元素。

選取若干路徑

通過在路徑表示式中使用“|”運算子,您可以選取若干個路徑。

例項

在下面的表格中,我們列出了一些路徑表示式,以及這些表示式的結果:

路徑表示式 結果
//book/title | //book/price 選取所有 book 元素的 title 和 price 元素。
//title | //price 選取所有文件中的 title 和 price 元素。
/bookstore/book/title | //price 選取所有屬於 bookstore 元素的 book 元素的 title 元素,以及文件中所有的 price 元素。

XPath 軸

軸可定義某個相對於當前節點的節點集。

軸名稱 結果
ancestor 選取當前節點的所有先輩(父、祖父等)
ancestor-or-self 選取當前節點的所有先輩(父、祖父等)以及當前節點本身
attribute 選取當前節點的所有屬性
child 選取當前節點的所有子元素。
descendant 選取當前節點的所有後代元素(子、孫等)。
descendant-or-self 選取當前節點的所有後代元素(子、孫等)以及當前節點本身。
following 選取文件中當前節點的結束標籤之後的所有節點。
namespace 選取當前節點的所有名稱空間節點
parent 選取當前節點的父節點。
preceding 選取文件中當前節點的開始標籤之前的所有節點。
preceding-sibling 選取當前節點之前的所有同級節點。
self 選取當前節點。

位置路徑表示式

位置路徑可以是絕對的,也可以是相對的。

絕對路徑起始於正斜槓( / ),而相對路徑不會這樣。在兩種情況中,位置路徑均包括一個或多個步,每個步均被斜槓分割:

絕對位置路徑:

/step/step/...

相對位置路徑:

step/step/...

每個步均根據當前節點集之中的節點來進行計算。

步(step)包括:

軸(axis)
定義所選節點與當前節點之間的樹關係
節點測試(node-test)
識別某個軸內部的節點
零個或者更多謂語(predicate)
更深入地提煉所選的節點集

步的語法:

軸名稱::節點測試[謂語]

例項

例子 結果
child::book 選取所有屬於當前節點的子元素的 book 節點
attribute::lang 選取當前節點的 lang 屬性
child::* 選取當前節點的所有子元素
attribute::* 選取當前節點的所有屬性
child::text() 選取當前節點的所有文字子節點
child::node() 選取當前節點的所有子節點
descendant::book 選取當前節點的所有 book 後代
ancestor::book 選擇當前節點的所有 book 先輩
ancestor-or-self::book 選取當前節點的所有book先輩以及當前節點(假如此節點是book節點的話)
child::*/child::price 選取當前節點的所有 price 孫。