小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

XML DOM Replace Nodes

 pengyan 2006-11-30

XML DOM Replace Nodes

prev next

Examples

In the examples below, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().

Replace a node in a nodelist
This example uses replaceChild() to replace the last child in a node list.

Replace data in a text node
This example uses replaceData() to replace data in a text node.


Replace a Node in a Node List

The replaceChild() method is used to replace a node in a node list.

The following code fragment creates a new <book> element that will replace the last <book> element:

//check if last child node is an element node
            function get_lastchild(n)
            {
            var x=n.lastChild;
            while (x.nodeType!=1)
            {
            x=x.previousSibling;
            }
            return x;
            }
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.documentElement;
//create a book element, title element and a text node
            var newNode=xmlDoc.createElement("book");
            var newTitle=xmlDoc.createElement("title");
            var newText=xmlDoc.createTextNode("A Notebook");
//add the text node to the title node,
            //and add the title node to the book node
            newTitle.appendChild(newText);
            newNode.appendChild(newTitle);
//replace the last node with the new node
            x.replaceChild(newNode,get_lastchild(x));

Note: Internet Explorer will skip white-space text nodes that are generated between nodes (e.g. new-line characters), while Mozilla will not. So, in the example above, the get_lastchild() function checks the node type of the last child node of the parameter.

Element nodes has a nodeType of 1, so if not the last child of the node in the parameter is an element node, it moves to the previous node, and checks if this node is an element node. This continues until the last child node (which must be an element node) is found. This way, the result will be correct in both Internet Explorer and Mozilla.


Replace Data In a Text Node

The replaceData() method is used to replace data in a text node.

The replaceData() method has three parameters:

  • offset - Where to begin replacing characters. Offset value starts at zero
  • length - How many characters to replace
  • string - The string to insert

The following code fragment will replace the eight first characters from the text node in the first <title> element with "Easy":

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


prev next

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多