🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will learn how to update or modify existing XML file in Java using JDOM Parser.
Development Process Steps
- Existing jdom_users.xml File
- Program to update existing XML file using JDOM Parser
- Updated updated_jdom_users.xml File
1. Existing jdom_users.xml File
Let’s say we have below source XML file. We will learn how to modify or edit this XML file with java program using JDOM parser.
<?xml version="1.0" encoding="UTF-8"?>
<Users>
<User id= "1">
<firstName>Ramesh</firstName>
<lastName>Fadatare</lastName>
<age>28</age>
<gender>Male</gender>
</User>
<User id= "2">
<firstName>John</firstName>
<lastName>Cena</lastName>
<age>45</age>
<gender>Male</gender>
</User>
<User id= "3">
<firstName>Tom</firstName>
<lastName>Cruise</lastName>
<age>40</age>
<gender>Male</gender>
</User>
</Users>
We want to change following for every User element in the XML.
- Update all firstName element to block letters.
- Append M for Male and F for Female in ID attribute.
- Remove gender element
- Add new element salary with default value 1000 for each user
2. Program to update existing XML file using JDOM Parser
Here is the java program that does all the above updates using JDOM Parser.
package net.javaguides.javaxmlparser.jdom;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class UpdateXMLFile {
public static void main(String[] args) throws JDOMException, IOException {
// we can create JDOM Document from DOM, SAX and STAX Parser Builder classes
String fileName = "jdom_users.xml";
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File(fileName);
Document jdomDoc = (Document) builder.build(xmlFile);
// Get list of User element
Element rootElement = jdomDoc.getRootElement();
List < Element > listUserElement = rootElement.getChildren("User");
// loop through to edit every User element
for (Element userElement: listUserElement) {
// change the name to BLOCK letters
String name = userElement.getChildText("firstName");
if (name != null)
userElement.getChild("firstName").setText(name.toUpperCase());
// edit the ID attribute based on Gender
String gender = userElement.getChildText("gender");
if (gender != null && gender.equalsIgnoreCase("female")) {
String id = userElement.getAttributeValue("id");
userElement.getAttribute("id").setValue(id + "F");
} else {
String id = userElement.getAttributeValue("id");
userElement.getAttribute("id").setValue(id + "M");
}
// remove gender element as it's not needed anymore
userElement.removeChild("gender");
// add salary element with default value to every user
userElement.addContent(new Element("salary").setText("1000"));
}
// document is processed and edited successfully, lets save it in new file
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
// output xml to console for debugging
// xmlOutputter.output(doc, System.out);
xmlOutputter.output(jdomDoc, new FileOutputStream("updated_jdom_users.xml"));
}
}
3. Updated or Modified updated_jdom_users.xml File
<?xml version="1.0" encoding="UTF-8"?>
<Users>
<User id="1M">
<firstName>RAMESH</firstName>
<lastName>Fadatare</lastName>
<age>28</age>
<salary>1000</salary>
</User>
<User id="2M">
<firstName>JOHN</firstName>
<lastName>Cena</lastName>
<age>45</age>
<salary>1000</salary>
</User>
<User id="3M">
<firstName>TOM</firstName>
<lastName>Cruise</lastName>
<age>40</age>
<salary>1000</salary>
</User>
</Users>
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business

Comments
Post a Comment
Leave Comment