XML (Extensible Markup Language) is a markup language used to store and transport data. Unlike HTML, XML doesn't have predefined tags or a predefined document structure. Instead, it allows you to create your own tags to define the data structure.
The XML Document Object Model (DOM) is an API for handling XML documents, allowing you to manipulate the document as a tree-like structure. In this tutorial, we will focus on inserting nodes before existing ones using JavaScript and the XML DOM.
š Note: XML DOM is supported by all modern web browsers.
Let's first create a simple XML document.
<books>
<book id="1">
<title>Book Title 1</title>
<author>Author 1</author>
</book>
<book id="2">
<title>Book Title 2</title>
<author>Author 2</author>
</book>
</books>To work with XML documents in JavaScript, we first need to load the XML file. We'll use the XMLHttpRequest object for this purpose.
const xhr = new XMLHttpRequest();
xhr.open('GET', 'books.xml', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// Parse the XML document and proceed with manipulation
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xhr.responseText, 'application/xml');
}
};
xhr.send();Now, let's insert a new book element before the existing one with id 1.
// Create a new book element
const newBook = xmlDoc.createElement('book');
newBook.setAttribute('id', '0');
// Create the title and author elements
const newTitle = xmlDoc.createElement('title');
newTitle.textContent = 'New Book Title';
const newAuthor = xmlDoc.createElement('author');
newAuthor.textContent = 'New Author';
// Append the new title and author elements to the new book element
newBook.appendChild(newTitle);
newBook.appendChild(newAuthor);
// Insert the new book element before the existing book with id 1
const existingBook = xmlDoc.getElementsByTagName('book')[0];
xmlDoc.insertBefore(newBook, existingBook);Let's put everything together and create a function to insert a new book before an existing one.
function insertBookBefore(title, author, id, existingId) {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'books.xml', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xhr.responseText, 'application/xml');
// Create a new book element
const newBook = xmlDoc.createElement('book');
newBook.setAttribute('id', id);
// Create the title and author elements
const newTitle = xmlDoc.createElement('title');
newTitle.textContent = title;
const newAuthor = xmlDoc.createElement('author');
newAuthor.textContent = author;
// Append the new title and author elements to the new book element
newBook.appendChild(newTitle);
newBook.appendChild(newAuthor);
// Get the existing book
const existingBook = xmlDoc.getElementsByTagName('book')[existingId];
// Insert the new book before the existing one
xmlDoc.insertBefore(newBook, existingBook);
// Output the modified XML
console.log(xmlDoc.documentElement.outerHTML);
}
};
xhr.send();
}
// Example usage
insertBookBefore('New Book Title', 'New Author', '0', '1');What is XML used for?
What is the XML Document Object Model (DOM)?
How do you create a new XML document in JavaScript?
How do you insert a new node before an existing one using the XML DOM?