请问读xml配置文件一般用什么方法?

thankyou 2004-05-11 05:22:48
?
...全文
76 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxf0088 2004-05-12
  • 打赏
  • 举报
回复
用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");
}
}
Arias 2004-05-12
  • 打赏
  • 举报
回复
可以根据一些java xml中的包来实现自己对xml文件的解析!
appleangle 2004-05-12
  • 打赏
  • 举报
回复
学习
whyxx 2004-05-12
  • 打赏
  • 举报
回复
网上有很多解析XML文件的第三方包,自己也可以写个简单的.
yayv 2004-05-12
  • 打赏
  • 举报
回复
sax足够
caoxmby 2004-05-11
  • 打赏
  • 举报
回复
dom+jaxp+xerces
wangdongzjk 2004-05-11
  • 打赏
  • 举报
回复
jdom

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧