1. 加載XML文檔:
var xmlDom = new ActiveXObject("MSXML2.DOMDocument");
xmlDom.load("filename.xml"); //加載XML文件
2. 訪問節點:
var root = xmlDom.documentElement;//獲取根節點
var nodeList = root.childNodes; //獲取節點的所有子節點
var node = nodeList[i];
var name = node.attributes[0].value;//獲取節點的第一個屬性的值
var xmlElement = node.xml;//包含起始標簽+內容+結束標簽
var content = xmlElement.childNodes[0].xml;//若xmlElement不包括子節點,則可以獲得xmlElement標簽中的內容;若其包括子節點,則獲得第一個子節點標簽及其內容;
3. 添加節點:
var newElement = xmlDom.createElement("element");
// 創建attribute屬性,并添加到element節點上
var attribute = xmlDom.createAttribute("attribute");
attribute.value = "attrubuteValue";
newElement.setAttributeNode(name);
// 創建subElement子節點,并添加到newElement節點上
var subElement = xmlDom.createElement("subElement");
newElement.text = "SubElementContent";
newElement.appendChild(subElement);
//將newElement添加到根節點下
root.appendChild(newElement);
4. 刪除節點:
var node = root.selectSingleNode("xpath");
if (node != null)
root.removeChild(node);
5. 保存節點:
xmlDom.save("driver:\\dir\filename.xml");//保存XML文件
6. Xpath幾個例子:
authors
authors/author
authors/author/name
authors/*/name
authors/author/* //*為通配符
authors/author[nationality]/name //用“[]”來限制只選取擁有nationality子節點的節點
authors/author[nationality='Russian']/name //進一步限制子節點nationality的值為'Russian'
authors/author[@period="classical"] //選取屬性period為"classical"的節點
authors/author/@period //選取節點的屬性
7. 介紹Xpath的兩個網址:
http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html
http://www.w3school.com.cn/xpath/index.asp
8. 一個解析Xpath的工具:

該工具的下載地址:http://www.axisebusiness.com/nleghari/visualxpath.zip
authors
浙公網安備 33010602011771號