使用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;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值