Introduction:
Working with XML files is a common task in Java development. However, there are instances where converting XML data into the Properties format becomes necessary for easier handling and better compatibility with various tools and libraries. In this blog, we will explore two simplified methods to achieve this conversion. We'll walk through the implementation of each method and provide detailed explanations to help you understand the process thoroughly.
Method 1: Using Java DOM Parser
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import java.util.Properties;
public class XmlToPropertiesUsingDOMParser {
public static void main(String[] args) {
try {
File xmlFile = new File("data.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("entry");
Properties properties = new Properties();
for (int temp = 0; temp < nodeList.getLength(); temp++) {
Node node = nodeList.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String key = element.getAttribute("key");
String value = element.getTextContent();
properties.setProperty(key, value);
}
}
// Save Properties to file
try (FileOutputStream output = new FileOutputStream("output.properties")) {
properties.store(output, "Converted from XML to Properties");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Let's assume we have an XML file named data.xml
with the following content:
<data>
<entry key="name">John Doe</entry>
<entry key="age">30</entry>
<entry key="email">[email protected]</entry>
</data>
After running the XmlToPropertiesUsingDOMParser
program, it will convert the XML data into Properties format and create a file named output.properties
with the following content:
# Converted from XML to Properties
name=John Doe
age=30
[email protected]
Method 2: Using Java Libraries (Apache Commons Configuration)
To use Apache Commons Configuration, you need to add the appropriate dependency to your project. Here's an example using Maven:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.7</version>
</dependency>
Now, let's proceed with the program:
import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import java.io.*;
public class XmlToPropertiesUsingApacheCommons {
public static void main(String[] args) {
try {
XMLConfiguration xmlConfig = new XMLConfiguration("data.xml");
PropertiesConfiguration propConfig = new PropertiesConfiguration();
propConfig.copy(xmlConfig);
// Save Properties to file
try (FileWriter writer = new FileWriter("output.properties")) {
propConfig.write(writer);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Using the same data.xml
file as before, the XmlToPropertiesUsingApacheCommons
program will generate the output.properties
file with the following content:
# Converted from XML to Properties
name=John Doe
age=30
[email protected]
Conclusion:
In this blog, we explored two simplified methods to convert XML to Properties format in Java. The first method used the Java DOM Parser to manually parse the XML data and create a Properties object. The second method leveraged the Apache Commons Configuration library to streamline the conversion process.
Comments (0)