请问读xml配置文件一般用什么方法? 网上有很多解析XML文件的第三方包,自己也可以写个简单的. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以根据一些java xml中的包来实现自己对xml文件的解析! 用SAX 和 Jdom都行,下面给出源码------SAX-----import org.xml.sax.Attributes;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"); }} new 出大量的静态内部类会不会造成方法区溢出? 怎么用java做一个带括号的计算器 Swing 使用LookAndFeel 后中文乱码。请哪个大哥帮帮忙 请教java中的图片上传下载(c/s结构) 出什么问题了?? 100分求一JSP系统问题 怎么在事件响应的时候响应消息? 如何取得浏览器当前选择的语言种类 一个简单问题 鼠标拖曳操作的问题 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");
}
}