使用common-configuration进行对配置文件的操作(xml,properties)

前端之家收集整理的这篇文章主要介绍了使用common-configuration进行对配置文件的操作(xml,properties)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

apache提供了commons-configuration-1.0.jar进行配置文件的操作。

可以在官网上下在common-configruation的jar包。

官方介绍:

The Commons Configuration software library provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources. Commons Configuration provides typed access to single,and multi-valued configuration parameters as demonstrated by the following code:


Double double = config.getDouble("number");
Integer integer getInteger

Configuration parameters may be loaded from the following sources:

  • Properties files
  • XML documents
  • Windows INI files
  • Property list files (plist)
  • JNDI
  • JDBC Datasource
  • System properties
  • Applet parameters
  • Servlet parameters
测试代码

<?xml version="1.0" encoding="UTF-8"?>
<pserson>
    <username>
        <zhangsan>
            11100141
        </zhangsan>
        <lisi>
            11100142
        </lisi>
    </username>

    <password>123456</password>
    <sex>male</sex>
    <address>beijingshichaoyangqu</address>
</pserson>

public class CommonConfigurationTest {

    public static void main(String[] args) {

        String resources = "commonConfigruationXML.xml";
        try {
            Configuration xml = new XMLConfiguration(resources);
            List list =xml.getList("username.zhangsan");
            System.out.println(list);

        } catch (ConfigurationException e) {
            e.printStackTrace();
        }


    }
}

属性文件进行的操作:

dbcp.username=root
dbcp.password=root
driver=com.MysqL.jdbc.Driver

package com.jd.list.utils;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 * Created by guojiangjiang on 2015/8/24.
 */
public class CommonConfigurationTest {

    public static void main(String[] args) {

        String resources = "commonConfigurationprop.properties";
        try {
            Configuration prop = new PropertiesConfiguration(resources);
            Object object =prop.getProperty("driver");
            System.out.println(object);

        } catch (ConfigurationException e) {
            e.printStackTrace();
        }

    }
}
原文链接:https://www.f2er.com/xml/296256.html

猜你在找的XML相关文章