节点对象属性
childNodes—返回节点到子节点的节点列表
firstChild—返回节点的首个子节点。
lastChild—返回节点的最后一个子节点。
nextSibling—返回节点之后紧跟的同级节点。
nodeName—返回节点的名字,根据其类型。
nodeType—返回节点的类型。
nodeValue—设置或返回节点的值,根据其类型。
ownerDocument—返回节点的根元素(document对象)。
parentNode—返回节点的父节点。
previousSibling—返回节点之前紧跟的同级节点。
text—返回节点及其后代的文本(IE独有)。
xml—返回节点及其后代的XML(IE独有)。
节点对象的方法
appendChild()—向节点的子节点列表的结尾添加新的子节点。
cloneNode()—复制节点。
hasChildNodes()—判断当前节点是否拥有子节点。
insertBefore()—在指定的子节点前插入新的子节点。
normalize()—合并相邻的Text节点并删除空的Text节点。
removeChild()—删除(并返回)当前节点的指定子节点。
replaceChild()—用新节点替换一个子节点。
IE6独有方法
selectNodes()—用一个XPath表达式查询
选择
节点。
selectSingleNode()—查找和XPath查询匹配的一个节点。
transformNode()—使用XSLT把一个节点转换为一个字符串。transformNodeToObject()—使用XSLT把一个节点转换成为一个文档。
--------------------------------------------------------------------------------------------------------------------------------------
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");