Last Updated: 25 Mar, 2025

Title - How to Read and Edit XML Files in Python, Java, and JavaScript

XML (Extensible Markup Language) is widely used for storing and transporting data. Whether you’re a beginner or an experienced developer, understanding how to read and edit XML files is essential. In this guide, we’ll cover basic and advanced methods to work with XML files efficiently.

Understanding XML Structure

An XML file consists of elements, attributes, and hierarchical data. Here’s a simple XML example:

<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="1">
<title>Introduction to XML</title>
<author>John Doe</author>
<price>29.99</price>
</book>
<book id="2">
<title>Advanced XML Techniques</title>
<author>Jane Smith</author>
<price>39.99</price>
</book>
</books>

Reading XML Files

1. Using Python

Python’s xml.etree.ElementTree module allows easy XML parsing.

xml.etree.ElementTree is a built-in module in Python, meaning you don’t need to install it separately. It comes pre-installed with Python and provides easy tools for parsing, modifying, and creating XML data.

Code Explanation

This Python script uses xml.etree.ElementTree to parse an XML file (books.xml). It retrieves all <book> elements, extracts their <title>, <author>, and <price> values, and prints them. This allows for easy reading of structured XML data.

import xml.etree.ElementTree as ET
tree = ET.parse('books.xml')
root = tree.getroot()
for book in root.findall('book'):
title = book.find('title').text
author = book.find('author').text
price = book.find('price').text
print(f"Title: {title}, Author: {author}, Price: {price}")

2. Using Java

Java provides javax.xml.parsers for XML handling.

javax.xml.parsers is a built-in Java package that provides classes for parsing XML using the DOM (Document Object Model) and SAX (Simple API for XML) parsers. It is included in the Java Standard Library, so you don’t need to install anything extra to use it.

Code Explanation

This Java program reads and parses an XML file (books.xml) using the DOM Parser. It first loads and normalizes the XML document, then retrieves all <book> elements. Inside a loop, it extracts and prints the text content of each <title> tag. This allows reading book titles from an XML file efficiently.

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class ReadXML {
public static void main(String[] args) throws Exception {
File inputFile = new File("books.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Title: " + element.getElementsByTagName("title").item(0).getTextContent());
}
}
}
}

3. Using JavaScript (Browser)

JavaScript’s DOMParser can parse XML.

JavaScript’s DOMParser is a built-in feature in modern web browsers. It allows you to parse XML strings into a structured Document Object Model (DOM), making it easy to navigate and manipulate XML data using JavaScript.

Code Explanation

This JavaScript code uses the DOMParser to parse an XML string (xmlString). It extracts the <title> element from the parsed XML document and logs its text content ("XML Guide") to the console. This is useful for handling XML data in JavaScript.

let xmlString = `<?xml version="1.0"?><books><book><title>XML Guide</title></book></books>`;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlString, "text/xml");
console.log(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue);

Editing XML Files

1. Editing with Python

This Python script uses xml.etree.ElementTree to parse and modify an XML file (books.xml). It finds a <book> element with an id of "1" and updates its <price> value to "24.99", then saves the changes back to the file.

import xml.etree.ElementTree as ET
tree = ET.parse('books.xml')
root = tree.getroot()
for book in root.findall('book'):
if book.get('id') == '1':
book.find('price').text = '24.99'
tree.write('books.xml')

2. Editing with Java

This Java program edits an XML file (books.xml) using the DOM parser. It loads and normalizes the document, locates the <book> element with id="1", updates its <price> value to "24.99", and saves the changes back to the file using a Transformer.

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class EditXML {
public static void main(String[] args) throws Exception {
File inputFile = new File("books.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if (element.getAttribute("id").equals("1")) {
element.getElementsByTagName("price").item(0).setTextContent("24.99");
}
}
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("books.xml"));
transformer.transform(source, result);
}
}

3. Editing with JavaScript

This JavaScript code parses an XML string using DOMParser, finds the <book> element with id="1", updates its <price> value to "24.99", and then converts the modified XML back to a string using XMLSerializer. Finally, it logs the updated XML.

let xmlString = `<?xml version="1.0"?><books><book id="1"><title>XML Guide</title><price>29.99</price></book></books>`;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlString, "text/xml");
let book = xmlDoc.getElementsByTagName("book")[0];
book.getElementsByTagName("price")[0].childNodes[0].nodeValue = "24.99";
let serializer = new XMLSerializer();
let updatedXML = serializer.serializeToString(xmlDoc);
console.log(updatedXML);

Conclusion

Reading and editing XML files is crucial for developers working with structured data. Python, Java, and JavaScript offer robust ways to process XML efficiently. Try these techniques and enhance your XML handling skills!