In the previous example, How do I load properties from XML file? we read properties from XML file. Now it’s the turn on how to store the properties as XML file.
package org.kodejava.io;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
public class PropertiesToXml {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("db.type", "mysql");
properties.setProperty("db.url", "jdbc:mysql://localhost:3306/kodejava");
properties.setProperty("db.username", "root");
properties.setProperty("db.password", "");
FileOutputStream fos = new FileOutputStream("db-config.xml");
properties.storeToXML(fos, "Database Configuration", StandardCharsets.UTF_8);
}
}
The saved XML file will look like the properties file below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Database Configuration</comment>
<entry key="db.type">mysql</entry>
<entry key="db.password"></entry>
<entry key="db.username">root</entry>
<entry key="db.url">jdbc:mysql://localhost:3306/kodejava</entry>
</properties>
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023