`

使用XMLConfiguration读取XML文件

阅读更多
包含XMLConfiguration的TestDataReader,用来读取XML文件,并能直接或者字符串,整形数据.要读取的XML文件格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<modules name="testData">

	<welcomeToPage>
		<patientURL>/patient/welcome.htm</patientURL>
		<welcomeMessageVP>Welcome to myBJC.org</welcomeMessageVP>
		<invalidPasswordVP>Unable to login</invalidPasswordVP>
		<errorPasswordVP>An error has occurred</errorPasswordVP>
        <randomQuestion>
			What is the name of your favorite author?=TomTom,
			What was your childhood nickname?=TBDTom,
			In what city did you meet your spouse or significant other?=STLTom,
			What school did you attend for sixth grade?=Maiden Tom
		</randomQuestion>			
	</welcomeToPage>
</modules>


TestDataReader代码如下:
import java.util.Properties;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.log4j.Logger;

/**
 * This class is used to parse an xml configuration file and return specified
 * value.
 */
public final class TestDataReader {
    private static final String KEY_CONNECTOR = ".";
    private static final String DATA_FILE_NAME = System
            .getProperty("user.dir")
            + "/src/test/resources/testData.xml";
    private static final Logger LOGGER = Logger
            .getLogger(TestDataReader.class);
    private static TestDataReader instance;
    private XMLConfiguration xmlConfig;

    private TestDataReader() {
        try {
            xmlConfig = new XMLConfiguration(DATA_FILE_NAME);
        } catch (ConfigurationException e) {
            LOGGER.error(e);
            throw new RuntimeException(e);
        }
    }

    /**
     * Read String type property.
     *
     * @param moduleName related module (second level node in xml).
     * @param propName   property name in xml file (third level node in xml ).
     * @return property of String type
     */
    public String getStringProperty(String moduleName, String propName) {
        return xmlConfig.getString(moduleName + KEY_CONNECTOR + propName);
    }

    /**
     * Read int type property.
     *
     * @param moduleName related module (second level node in xml).
     * @param propName   property name in xml file (third level node in xml ).
     * @return property of int type
     */
    public int getIntProperty(String moduleName, String propName) {
        return xmlConfig.getInt(moduleName + KEY_CONNECTOR + propName);
    }

    /**
     * Read property from hash table.
     *
     * @param moduleName related module (second level node in xml).
     * @param propName   property name in xml file (third level node in xml ).
     * @param eleName    key value of the hash table.
     * @return property of int type
     */
    public String getHashProperty(String moduleName, String propName,
                                  String eleName) {
        // property contains a list key/value pairs
        Properties property = xmlConfig.getProperties(moduleName
                + KEY_CONNECTOR + propName);

        // Look up the list and get the specified value
        return property.get(eleName).toString();
    }

    /**
     * Generate identical instance.
     *
     * @return identical instance of TestDataReader
     * @throws ConfigurationException ConfigurationException when instantiating
     * TestDataReader.
     */
    public static TestDataReader getInstance() {
        if (instance == null) {
            instance = new TestDataReader();
        }
        return instance;
    }

}
1
0
分享到:
评论

相关推荐

    org.apache.commons.configuration.XMLConfiguration的读取范例

    org.apache.commons.configuration.XMLConfiguration的读取范例

    commons-configuration代码实例

    commons configuration读取配置文件的例子,包括properties文件,ini文件和xml文件

    Configutron - xml configuration module-开源

    Configutron是基于XML的通用应用程序配置模块。 它为应用程序开发人员提供了定义和加载基于XML的应用程序配置的功能,而无需编写一行Java代码来解析和读取配置文件。

    详解C#如何读写config配置文件

    应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。配置文件的根节点是configuration。我们经常访问的是...

    commons-configuration2-2.1-bin.zip

    configuration包读取配置文件,配置文件一般常见的有两种:键值对格式,或XML配置文件,读取这类配置文件可以用Commons Configuration包。

    文件大小读取程序源码

    using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; ...

    Common-Configuration

    Common-Configuration例子,包含了读取xml和properties文件。

    .NetCore获取Json和Xml格式的配置信息

    本篇将和大家分享的是如何获取Json和Xml格式的配置信息,主要介绍的是Configuration扩展方法的使用,对.netcore 获取json和xml格式的配置信息的相关知识,感兴趣的朋友一起看看吧

    c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法

    我们经常会希望在程序中写入一些...应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。配置文件的根节点是config

    .NET Core读取配置文件方式详细总结

    基于.NET Core的跨平台开发,配置文件与之前.NET Framework采用xml的config文件不同,目前主要是采用json文件键值对配置方式读取。 参考网上相关资料总结如下: 一. 引入扩展 System.Configuration.Configuration...

    C#中读取App.config配置文件代码实例

    App.config是C#开发WinForm程序的...本文介绍App.config的简介使用。 我们先来打开一个App.config文件,看看它的内容像什么样子。 &lt;?xml version=1.0 encoding=utf-8 ?&gt; &lt;configuration&gt; &lt;add key

    Log4net详细说明使用

    也可以使用自定义的配置文件,具体请参见4.4 关联配置文件。 第三步:修改配置文件。如果是CS程序,则在默认的App.config文件(没有新建一个)中添加内容;如果是BS程序,则添加到Web.config文件中,添加内容一样,...

    使用linq to xml修改app.config示例(linq读取xml)

    代码如下:Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.AppSettings.Settings[“节点名称”].Value =”0″; configuration.Save...

    apache-commons源码及jar文件

    Commons-Configuration 工具对各种各式的配置和参考文件提供读取帮助. Daemon 一种 unix-daemon-like java 代码的替代机制 DBCP Commons-DBCP 提供数据库连接池服务 DbUtils DbUtils 是一个 JDBC helper 类库,...

    Mybatis源码分析.md

    &lt;!-- TOC --&gt; - Mybatis源码分析 - 1. 解析配置文件,创建SQLSessionFactory - 2.... - 3.... - 4....1. 解析配置文件,创建...这一步首先读取了mybatis的configuration xml配置文件,用这个流构造了Factory的Builder,它底

    ASP.NET(C#)应用程序配置文件app.config/web.config的增、删、改操作

    配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET 2.0 开始,就非常方便了,提供了 System [.Web] .Configuration 这个管理功能的NameSpace,要使用它,需要添加对 ...

    TinyConfiguration:TinyConfiguration是一个简单轻巧的库,用于管理配置文件(JSON,XML,CSV和YAML)

    读取,写入和删除自定义配置文件(甚至异步) 轻松搜索和检索值使用Lambda函数对属性值执行验证将配置导出为JSON , XML , YAML和CSV快速开始基础知识import org.tinyconfiguration.imp.basic.Configuration ;...

    javaproperties:用于读写Java .properties文件的Python库

    javaproperties通过基于json模块的简单API提供对读写(简单的面向行格式和XML)的支持-尽管,为了恢复Java迷,它还包括旨在匹配行为的Properties类。 Python 尽可能多地使用 。 javaproperties版本的javaproperties...

    定时移除指定表的重复信息

    此模块使用的commons-configuration 来实现读取xml配置文件,使用quartz来实现定时任务,根据配置文件的配置信息来实现对指定表的信息重复数据的移除。

    C#实现的XML操作类实例

    这里讲述了C#写的一个XML操作类,包括读取/插入/修改/删除。 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System....

Global site tag (gtag.js) - Google Analytics