XML DOM Append Child 🎯

beginner
10 min

XML DOM Append Child 🎯

Welcome to our comprehensive tutorial on XML DOM Append Child! By the end of this lesson, you'll have a solid understanding of how to dynamically manipulate XML documents using JavaScript's Document Object Model (DOM).

What is XML DOM Append Child? 📝

In the context of XML, the Append Child method is used to insert a new child node as the last child of an existing element. This method is essential for dynamically building and modifying XML documents within your JavaScript applications.

Why use XML DOM Append Child? 💡

XML DOM Append Child is crucial when you need to create or modify XML documents on the fly. For example, you might want to:

  • Build an XML file to send data to a server
  • Dynamically change the content of an XML document in response to user actions
  • Parse and manipulate XML data received from an API or file

Getting Started 🔧

Before we dive into the code, let's ensure you have the necessary setup:

  1. A modern web browser (Chrome, Firefox, Edge, etc.)
  2. A text editor or IDE of your choice (Visual Studio Code, Sublime Text, Atom, etc.)

Creating an XML Document 📝

First, let's create a simple XML document to work with:

xml
<books> <book id="001"> <title>XML Programming</title> <author>Elliotte Rusty Harold</author> <price>49.95</price> </book> <book id="002"> <title>Learning XML</title> <author>Erik T. Ray</author> <price>39.95</price> </book> </books>

Save this as books.xml in your project folder.

Loading an XML Document 📝

Next, we'll load this XML document into JavaScript using the built-in DOMParser:

javascript
const xmlData = ` <books> <!-- XML content here --> </books> `; const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlData, "text/xml");

Accessing XML Elements 💡

Now that we have our XML document loaded, we can access its elements using the getElementsByTagName() method:

javascript
const books = xmlDoc.getElementsByTagName("books")[0];

Creating New Elements 💡

To create new elements, we'll use the createElement() method:

javascript
const newBook = xmlDoc.createElement("book");

Adding Attributes 💡

To add attributes to our new element, we'll use the setAttribute() method:

javascript
newBook.setAttribute("id", "003");

Creating Child Elements 💡

Now, let's create some child elements for our new book:

javascript
const title = xmlDoc.createElement("title"); title.textContent = "JavaScript and XML"; const author = xmlDoc.createElement("author"); author.textContent = "Marcus Zarra"; const price = xmlDoc.createElement("price"); price.textContent = "59.95";

Appending Child Elements 💡

Finally, we'll append our child elements to the new book element and the new book to the books element:

javascript
newBook.appendChild(title); newBook.appendChild(author); newBook.appendChild(price); books.appendChild(newBook);

Saving the Modified XML Document 📝

To save the modified XML document, we'll use the querySelector() method to select the root element and innerHTML to get the XML as a string:

javascript
const xmlString = xmlDoc.querySelector("books").innerHTML; console.log(xmlString);

Quiz 📝

Question: What method is used to insert a new child node as the last child of an existing element in XML DOM?

A: InsertLastChild() B: AppendChild() C: AddChild()

Correct: B Explanation: The AppendChild() method is used to insert a new child node as the last child of an existing element in XML DOM.


That's it for this tutorial! With the knowledge you've gained, you can now dynamically manipulate XML documents using JavaScript's DOM. Happy coding! 🥳💻