🎓 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
XML (Extensible Markup Language) is a widely used format for storing and transporting data, and Java provides several ways to parse XML documents. One common requirement in Java applications is to parse an XML document and store its contents in a HashMap for easy access and manipulation. This process can be achieved using various libraries and techniques. In this blog post, we will explore a practical method to parse XML data into a HashMap in Java.
Why Use HashMap for XML Data?
A HashMap in Java is a collection used to store key-value pairs. When dealing with XML data, you often need to access specific elements or attributes efficiently. Storing XML data in a HashMap allows for quick retrieval of values based on keys, which can represent XML tags or attributes.
Using JDOM for XML Parsing
For this example, we'll use the JDOM library, a popular and easy-to-use library for parsing XML in Java. JDOM provides a way to represent XML documents in Java and offers a user-friendly API to access and manipulate the data.
Step 1: Add JDOM Dependency
First, add the JDOM dependency to your project. If you are using Maven, include the following in your pom.xml:
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
Step 2: Sample XML Data
<root>
<element key="1">Value 1</element>
<element key="2">Value 2</element>
<element key="3">Value 3</element>
</root>
Step 3: Parse XML and Populate HashMap
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class XMLToHashMap {
public static void main(String[] args) {
try {
File inputFile = new File("sample.xml");
SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(inputFile);
Element rootElement = document.getRootElement();
Map<String, String> xmlDataMap = new HashMap<>();
List<Element> elements = rootElement.getChildren();
for (Element element : elements) {
String key = element.getAttributeValue("key");
String value = element.getText();
xmlDataMap.put(key, value);
}
System.out.println("XML Data Map: " + xmlDataMap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
XML Data Map: {1=Value 1, 2=Value 2, 3=Value 3}
In this code: - We use SAXBuilder to parse the XML file.
- We iterate over the child elements of the root element.
- For each element, we retrieve the value of the key attribute and the text content and then store them in the HashMap.
Conclusion
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
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment