XML DOM Replace Node
- Previous page DOM delete node
- Next page DOM create node
Ang gumagamit ng paraan na replaceChild() ay para mapalitan ang tinukoy na node.
Ang attribute na nodeValue ay para mapalitan ang teksto ng text node.
Ang halimbawa
Ang sumusunod na halimbawa ay gumagamit ng XML na file books.xml.
Ang function loadXMLDoc(),na matatagpuan sa labas ng JavaScript, ay para maglunsad ng XML na file.
- Mapalitan ang elemento na node
- Ang halimbawa na ito ay gumagamit ng replaceChild() para mapalitan ang unang <book> na node.
- Mapalitan ang datos ng text node
- Ang halimbawa na ito ay gumagamit ng attribute na nodeValue para mapalitan ang datos ng text node.
Mapalitan ang elemento na node
Ang gumagamit ng paraan na replaceChild() ay para mapalitan ang node.
Ang sumusunod na klip ng kodigo ay para mapalitan ang unang <book> na elemento:
xmlDoc=loadXMLDoc("books.xml"); x = xmlDoc.documentElement; //Lumikha ng isang elemento na book, isang elemento na title, at isang text na node newNode = xmlDoc.createElement("book"); newTitle = xmlDoc.createElement("title"); newText = xmlDoc.createTextNode("Hello World"); //Magdagdag ng text na node sa title na node newTitle.appendChild(newText); //Magdagdag ng title na node sa book na node newNode.appendChild(newTitle); y = xmlDoc.getElementsByTagName("book")[0]; //Gumamit ng bagong node na ito para mapalitan ang unang book na node x.replaceChild(newNode, y);
Example explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Lumikha ng bagong elemento na <book>
- Lumikha ng bagong elemento na <title>
- Lumikha ng bagong text node na may teksto "Hello World"
- Magdagdag ng bagong text node sa bagong elemento na <title>
- Magdagdag ng bagong elemento na <book> sa bagong elemento na <book>
- Gumamit ng bagong elemento na <book> para mapalitan ang unang elemento na <book>
Mapalitan ang datos ng text node
Ang gumagamit ng paraan na replaceData() ay para mapalitan ang datos ng text node.
The replaceData() method has three parameters:
- offset - Where to start replacing characters. The offset value starts at 0.
- length - How many characters to replace
- string - The string to be inserted
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.replaceData(0,8,"hello");
Example explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Get the text node of the first <title> element node
- Use the replaceData method to replace the first 8 characters of the text node with "hello"
Use the nodeValue property
It's easier to replace text node data with the nodeValue property.
The following code snippet will replace the text node value of the first <title> element with "Easy Italian":
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Hello World";
Example explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Get the text node of the first <title> element node
- Use the nodeValue property to change the text of this text node
You can Change node Read more about changing node values in this section.
- Previous page DOM delete node
- Next page DOM create node