网上有很多解析XML文件的第三方包,自己也可以写个简单的.
解决方案 »
- [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用户 'ppc' 登录失败。
- Android 数据储存之 Shared Preferences 之惑
- 请教:java连接数据库
- java工程打包后,找不到依赖的jar包。求助如何设置manifest.mf文件
- 关于scanner控制台输入输出问题
- report分页时,打印预览时为什么第二页的内容会在第一页显示?
- swing中有几种open source架构!
- 如何在Eclipes中开发j2se程序,新人,别见笑,有经验的多指点
- 本人菜鸟刚学习hibernate 在练习对数据添加数据时遇到问题实在解决不了求高手帮忙
- 请问各位大虾policy文件及property文件如何生成?
- jdk如何反编译
- <100分>新手挑战Hodex的那篇<<篮球中的exception>> 新手都来啊! Hodex大哥也进来!!
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import java.util.Properties;public class ConfigurationParser extends DefaultHandler { private Properties props; private String currentSet;
private String currentName;
private StringBuffer currentValue = new StringBuffer(); public ConfigurationParser() {
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 ParserConXML { public ParserConXML() {
} public Properties getProps() {
Properties props = new Properties();
SAXParser parser;
ConfigurationParser handler = new ConfigurationParser(); //获取SAX工厂对象
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false); try
{
//获取SAX解析
parser = factory.newSAXParser();
//将解析器和解析对象联系起来,开始解析
parser.parse("configuration.xml", handler);
props = handler.getProps();
}catch(Exception e){
}
finally{
factory=null;
parser=null;
handler=null;
}
return props;
}
/*
public static void main(String[] args) {
ParserConXML test = new ParserConXML();
Properties prop = test.getProps();
System.out.println("-------"+prop.getProperty("db_url"));
}
*/
}
---------JDOM-----------import org.jdom.*;
import org.jdom.input.*;public class XMLConfigParser {
private Element elementRoot;
public XMLConfigParser() {
try {
SAXBuilder saxbuilder = new SAXBuilder();
Document doc = saxbuilder.build("configration.xml");
elementRoot = doc.getRootElement();
}
catch (Exception e) {
e.printStackTrace();
}
} public Element getChildElement(String elementName){
return elementRoot.getChild(elementName);
}
}import org.jdom.*;public class InitParameter {
private static XMLConfigParser xmlConfigParser = new XMLConfigParser();
private static Element connPoolE =
xmlConfigParser.getChildElement("connectionpool"); public InitParameter() {
} public static String getUrl() {
return connPoolE.getChildTextTrim("url");
} public static String getDatasource() {
return connPoolE.getChildTextTrim("datasource");
}
}