Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing XML

XML DOM Replace Nodes:



Replacing nodes in an XML DOM involves selecting the node you want to replace and then inserting a new node in its place within the document structure. Here’s a simple explanation with an example:

Example XML Document

Consider the following XML document (data.xml):

           

Example

<library> <book> <title>Harry Potter</title> <author>J.K. Rowling</author> </book> <book> <title>The Hobbit</title> <author>J.R.R. Tolkien</author> </book> <book> <title>1984</title> <author>George Orwell</author> </book> </library>

Replacing Nodes Using JavaScript (for Web Environment)

If you're working with JavaScript in a web environment, you can use the browser's DOM API to manipulate XML documents. Here’s how you can replace a <book> node in the XML:

            

Example

// Load the XML document var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var xmlDoc = this.responseXML; // Select the node you want to replace (assuming the first book) var books = xmlDoc.getElementsByTagName("book"); var bookToReplace = books[0]; // Create a new node var newBook = xmlDoc.createElement("book"); var newTitle = xmlDoc.createElement("title"); var newAuthor = xmlDoc.createElement("author"); var newTitleText = xmlDoc.createTextNode("Lord of the Rings"); var newAuthorText = xmlDoc.createTextNode("J.R.R. Tolkien"); newTitle.appendChild(newTitleText); newAuthor.appendChild(newAuthorText); newBook.appendChild(newTitle); newBook.appendChild(newAuthor); // Replace the old node with the new one bookToReplace.parentNode.replaceChild(newBook, bookToReplace); // Output the modified XML console.log(xmlDoc.documentElement.outerHTML); } }; xhttp.open("GET", "data.xml", true); xhttp.send();

Explanation

  1. Load the XML Document: Use XMLHttpRequest to load the XML file (data.xml in this case).

  2. Select Node to Replace: Use getElementsByTagName() to get all <book> elements. Here, we select the first <book> element (books[0]) as the one we want to replace.

  3. Create New Node: Use createElement() to create a new <book>, <title>, and <author> elements. Use createTextNode() to set the text content for the <title> and <author>.

  4. Replace Node: Use parentNode.replaceChild() to replace the old <book> node (bookToReplace) with the new <book> node (newBook).

  5. Output: Finally, you can log or use xmlDoc.documentElement.outerHTML to see the modified XML structure.

Important Notes

By following these steps, you can effectively replace nodes in an XML DOM structure using JavaScript. Adjust the specific nodes and content according to your XML structure and requirements.




Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java