根据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