解析配置文件

4 篇文章 0 订阅

根据solr源码进行裁剪,解析配置文件中的内容。

比如配置文件search.xml

<?xml version="1.0" encoding="UTF-8" ?>
<search persistent="false" sharedLib="lib">

	<cores shareSchema="false" defaultCoreName="core0" profilerUpdateTime="100">
		<core name="core0" instanceDir="." dataDir="./data" config="solrconfig.xml" />
	</cores>

</search>

我们想将其中的dataDir、config的值解析出来,怎么做呢?根据solr源码,我们实现了Config.java  DOMUtil.java 类,分别来加载和解析配置文件的内容

测试主函数:

package com.netboy.demo;

import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

/**
 * @功能:
 * @版本号:
 * @作者: netboy Email:wang_lin_n@163.com
 * @时间: 2013-3-9 上午01:03:22
 * @例子:
 */
public class MoreMain {

	/**
	 * @param args
	 * @throws SAXException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws XPathExpressionException
	 */
	public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
		String configFile = "./conf/search.xml";
		FileInputStream cfgis = new FileInputStream(configFile);
		Config conf = new Config("search.xml", cfgis, null);

		String libDir = conf.get("search/@sharedLib", null);
		String coreName = conf.get("search/cores/@defaultCoreName", null);
		int UpdateTime = conf.getInt("search/cores/@profilerUpdateTime", -1);
		String dataDir = conf.get("search/cores/core/@dataDir", null);
		String configName = conf.get("search/cores/core/@config", null);
		
		System.out.println("the libDir is : " + libDir);
		System.out.println("the coreName is :" + coreName);
		System.out.println("the UpdateTime is :" + UpdateTime);
		System.out.println("the dataDir is :" + dataDir);
		System.out.println("the config is :" + configName);
	}

}

运行结果:

the libDir is : lib
the coreName is :core0
the UpdateTime is :100
the dataDir is :./data
the config is :solrconfig.xml

具体工程代码可以在这里下载: http://download.csdn.net/detail/yeshenrenjin/5123022

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值