读取xml配置文件

以下是读取自定义xml文件的工具类,

import java.util.Properties;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ConfigParser extends DefaultHandler {

//定义一个Properties 用来存放属性值
private Properties props;
private String currentSet;
private String currentName;
private StringBuffer currentValue = new StringBuffer();
// 构建器初始化props
public ConfigParser() {
this.props = new Properties();
}
public Properties getProps() {
return this.props;
}
// 定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来.
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentValue.delete(0, currentValue.length());
this.currentName = qName;
}
// 这里是将<xxx></xxx>之间的值加入到currentValue
public void characters(char[] ch, int start, int length) throws
SAXException {
currentValue.append(ch, start, length);
}
// 在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中
public void endElement(String uri, String localName, String qName) throws
SAXException {
props.put(qName.toLowerCase(), currentValue.toString().trim());
}



}


import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class ParseXML {

// 定义一个Properties 用来存放属性值
private Properties props;

public Properties getProps() {
return this.props;
}

public void parse(String filename) throws Exception {
// 将我们的解析器对象化
ConfigParser handler = new ConfigParser();
// 获取SAX工厂对象
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
// 获取SAX解析
SAXParser parser = factory.newSAXParser();
try {
// 将解析器和解析对象xml联系起来,开始解析
parser.parse(filename, handler);
// 获取解析成功后的属性
props = handler.getProps();
} finally {
factory = null;
parser = null;
handler = null;
}
}

}


import java.util.Properties;

public class ReadConfigXml {

private Properties props;

public ReadConfigXml(String url) {
ParseXML myRead = new ParseXML();
try {
myRead.parse(url);
props = new Properties();
props = myRead.getProps();
} catch (Exception e) {
e.printStackTrace();
}
}

public String getindex() {
return props.getProperty("index");
}

public String getdata() {
return props.getProperty("data");
}

public String gethttp() {
return props.getProperty("http");
}

public int getShow() {
return Integer.parseInt(props.getProperty("show"));
}

}

public class XmlBase {

private String CONFIG = "Config.xml"; //定义常量.即XML配置文件的名称.
String Path = XmlBase.class.getResource("/") + CONFIG; //获取当前XML文件所在的路径
ReadConfigXml xml = new ReadConfigXml(Path);

public XmlBase() {

}

public static XmlBase newInstance() {
return new XmlBase();
}

/*
* 获取索引文件夹位置
*/
public String getIndex_path() {
String index = xml.getindex();
return index;
}

/*
* 获取文件所在的文件包
*/
public String getData_path() {
String data = xml.getdata();
return data;
}

/*
* 获取域名
*/
public String getHttp() {
String http = xml.gethttp();
return http;
}

/*
* 获取简要内容显示字数
*/
public int show() {
int show = xml.getShow();
return show;
}

}


xmldemo

<?xml version="1.0" encoding="UTF-8"?>
<reportenv>
<datasource>
<index>\test\index</index>
<data>\test\data</data>
<http>http://www.zzu.com</http>
<show>200</show>
</datasource>
</reportenv>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值