XML 和 Properties的相互转换

1.0 How to convert properties file into XML file – Java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesXMLExample
{
    public static void main(String[] args) throws IOException
    {
        Properties props = new Properties();
        props.setProperty("email.support", "donot-spam-me@nospam.com");

        //where to store?
        OutputStream os = new FileOutputStream("c:/email-configuration.xml");

        //store the properties detail into a pre-defined XML file
        props.storeToXML(os, "Support Email","UTF-8");

        System.out.println("Done");
    }
}

The above example will write the properties detail into a XML file “c:/email-configuration.xml“.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
        <comment>Support Email</comment>
        <entry key="email.support">donot-spam-me@nospam.com</entry>
    </properties>
2.0 How to convert XML file into properties file – Java
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
   <properties>
    <comment>Support Email</comment>
    <entry key="email.support">donot-spam-me@nospam.com</entry>
   </properties>
package com.mkyong;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesXMLExample
{
    public static void main(String[] args) throws IOException
    {
        Properties props = new Properties();

        InputStream is = new FileInputStream("c:/email-configuration.xml");
        //load the xml file into properties format
        props.loadFromXML(is);

        String email = props.getProperty("email.support");

        System.out.println(email);

    }
}

Output
The above example will print out the value of properties key : “email.support” :

donot-spam-me@nospam.com

  
xml在线校验、转JSON、格式化站点
https://codebeautify.org/xmlviewer

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Properties 文件和 XML 文件都是常用的配置文件格式,用于存储应用程序的配置信息。它们之间的主要区别如下: 1. 格式不同 Properties 文件使用简单的 key-value 格式,每一行表示一个属性。例如: ``` database.driver=com.mysql.cj.jdbc.Driver database.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC database.username=root database.password=123456 ``` XML 文件采用标签和属性的方式来表示配置信息。例如: ``` <database> <driver>com.mysql.cj.jdbc.Driver</driver> <url>jdbc:mysql://localhost:3306/test?serverTimezone=UTC</url> <username>root</username> <password>123456</password> </database> ``` 2. 扩展性不同 XML 文件比 Properties 文件更加灵活和可扩展,可以定义更加复杂的数据结构。XML 文件支持嵌套、属性、命名空间等特性,适用于更加复杂的配置场景。而 Properties 文件只能支持简单的 key-value 格式,不支持嵌套和复杂的数据结构。 3. 可读性不同 XML 文件比 Properties 文件更加易读,可以定义更加有意义的标签和属性名。而 Properties 文件只能使用简单的键值对,不太容易理解和维护。 4. 读取方式不同 Java 中可以使用 Properties 类来读取和写入 Properties 文件,也可以使用 DOM 或 SAX 解析器来读取和写入 XML 文件。 总之,Properties 文件适用于简单的配置场景,而 XML 文件适用于更加复杂的配置场景。在实际开发中,应根据具体的需求选择适合的配置文件格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值