1. 程式人生 > >js訪問xml之節點操作(轉)

js訪問xml之節點操作(轉)

本文轉自:https://www.cnblogs.com/Caceolod/articles/1753117.html

程式碼



Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->獲取元素值


x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
txt=y.nodeValue;
結果:txt = "Everyday Italian"


獲取屬性值 - getAttribute()


xmlDoc=loadXMLDoc("books.xml");
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
結果:txt = "en"


獲取屬性值 - getAttributeNode()


x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");
txt=x.nodeValue;


改變文字節點得值


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc .getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";


改變屬性的值


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc .getElementsByTagName("book");
x[0].setAttribute("category","food");


刪除元素節點


xmlDoc = loadXMLDoc("books.xml");
y = xmlDoc.getElementsByTagName("books")[0];
xmlDoc.documentElement.removeChild(y);


刪除自身


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);


刪除文字節點


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
x.removeChild(y);


清空文字節點


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("title")[0];
x.nodeValue = "";


根據名稱刪除屬性節點


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("book");
x[0].removeAttribute('category');


根據物件刪除屬性節點


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("book");
for(var i=0;i<x.length;i++){
       while(x[i].attributes.length>0){
                attnode=x[i].attributes[0];
                old_att=x[i].removeAttributeNode(attnode);
       }
}

x[0].removeAttribute(category);

程式碼


Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->替換元素節點


 replaceChild() 方法用於替換節點。
xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.documentElement;


//create a book element, title element and a text node
newNode = xmlDoc.createElement("book");
newTitle = xmlDoc.createElement("title");
newText = xmlDoc.createTextNode("A Notebook");


//add the text node to the title node,
newTitle.appendChild(newText);
//add the title node to the book node
newNode.appendChild(newTitle);


y = xmlDoc.getElementsByTagName("book")[0];
//replace the first book node with the new node
x.replaceChild(newNode,y);


替換文字節點中的資料


replaceData()方法用於替換文字節點中的資料。
replaceData()方法有三個引數:
offset-在何處開始替換字元。Offset值以0開始。
length-要替換多少字元。
string-要插入的字串。


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.replaceDate(0,8,"Easy");


用nodeValue屬性來替換文字節點中的資料。
xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Italian";


建立新的元素節點


createElement()方法建立新的元素節點。
xmlDoc = loadXMLDoc("books.xml");
newNode = xmlDoc.createElement("edition");
x = xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newNode);


建立新的屬性節點


xmlDoc = loadXMLDoc("books.xml");
newAtt = xmlDoc.createAttribute("edition");
newAtt.nodeValue ="first";
x = xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newAtt);


通過setAttribute建立屬性。
xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("book");
x[0].setAttribute("edition","first");


 建立文字節點


createTextNode()方法建立新的文字節點。
xmlDoc = loadXMLDoc("books.xml");
newNode = xmlDoc.createElement("publisher");
newText = xmlDoc.createTextNode("english");
newNode.appendChild(newText);
x = xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newNode);


建立一個CDATA Section節點


xmlDoc = loadXMLDoc("books.xml");
newCDATA = xmlDoc.createCDATASection("Special offer & Book Sale");
x = xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);


建立註釋節點


xmlDoc = loadXMLDoc("books.xml");
newComment = xmlDoc.createComment("Revised March 2008");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);


新增節點


xmlDoc = loadXMLDoc("books.xml");
newNode = xmlDoc.createElement("publisher");
x = xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newNode);


插入節點--insertBefore()


xmlDoc = loadXMLDoc("books.xml");
newNode = xmlDoc.createElement("book");
x = xmlDoc.documentElement;
y = xmlDoc.getElementsByTagName("book")[3];
x.insertBefore(newNode,y);


新增新屬性


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("book");
x[0].setAttribute("edition","first");


向文字節點新增文字--insertData()


xmlDoc = loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.insertData(0,"Easy");


複製節點


xmlDoc = loadXMLDoc("books.xml");
oldNode = xmlDoc.getElementsByTagName("book")[0];
newNOde = oldNode.cloneNode(true);
xmlDoc.documentElement.appendChild(newNode);
//output all titles
y = xmlDoc.getElementsByTagName("title");
for(var i=0;i<y.length;i++){
     document.write(y[i].childNodes[0].nodeValue);
     document.write("<br/>");
}