日期:2014-4-11 浏览次数:20277次

一个读取xml文件内容的类
package project.util.xml;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import org.jdom.*;
import org.jdom.input.*;

/**
* <p>Title: <font color="steelblue" size="10">读取xml文件信息</font></p>
* <p>Description: <font color="steelblue">从XML配置文件中获得配置信息。excerpt form jdom。</font></p>
* <p>Copyright: <font color="steelblue">Copyright (c) 2004</font></p>
* <p>Company: <font color="steelblue">Harmonious</font></p>
* @author <font color="steelblue">TZL</font>
* @version <font color="steelblue">1.0</font>
*/

public class XMLReader {
/*
#设置根的输出配置,格式为 "info [2004-05-01 22:35:30] [name]logname(b.c) [line] 86 msg-->log信息"
log4j.rootLogger=DEBUG, rootAppender
log4j.appender.rootAppender=org.apache.log4j.RollingFileAppender
log4j.appender.rootAppender.File=e:/MapXtremeSmpl.log
log4j.appender.rootAppender.MaxFileSize=1000KB
log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.rootAppender.layout.ConversionPattern=%-5p [%d{yyyy-mm-dd HH:mm:ss}] [name] %c{2} [line] %L msg--> %m%n
*/
static public Logger log = Logger.getLogger(XMLReader.class);
protected Element m_RootElement = null;
protected String m_webAppPath = null;

/**
* <font color="orange">结构函数。</font>
* @param xmlFile <font color="steelblue">要读取的配置文件的绝对路径。</font>
*/
public XMLReader(String xmlFile) {
m_webAppPath = null;
try {
PatternLayout layout = new PatternLayout("%-5p %d{yyyy-MM-dd HH:mm:ss} [name] %c{2} [line] %L [msg] %m%n");
ConsoleAppender appender = new ConsoleAppender(/*new SimpleLayout(),*/layout, "System.err");
log.addAppender(appender);

SAXBuilder builder = new SAXBuilder();
document.nbspdoc = null;
doc = builder.build(new FileInputStream(xmlFile));
m_RootElement = doc.getRootElement();
}
catch (IOException ex) {
log.error("XMLReader结构时出现IO错误:" + ex.toString());
}
catch (JDOMException ex1) {
log.error("XMLReader结构时分析XML文件出错:" + ex1.toString());
}
catch (Exception ex) {
log.error("XMLReader 结构出错:" + ex.toString());
}
}

/**
* <font color="orange">结构函数。配置文件必须指定为发布的使用的根目录下的/XmlConfig/Config.xml。</font>
* @param servletObj <font color="steelblue">随便一个HttpServlet对象。</font>
*/
public XMLReader(HttpServlet servletObj) {
m_webAppPath = servletObj.getServletContext().getRealPath("/");
String configFileName = m_webAppPath + "XmlConfig/Config.xml";

try {
PatternLayout layout = new PatternLayout("%-5p %d{yyyy-MM-dd HH:mm:ss} [name] %c{2} [line] %L [msg] %m%n");
ConsoleAppender appender = new ConsoleAppender( /*new SimpleLayout(),*/layout, "System.err");
log.addAppender(appender);

SAXBuilder builder = new SAXBuilder();
document.nbspdoc = null;
doc = builder.build(new FileInputStream(configFileName));
m_RootElement = doc.getRootElement();
}
catch (IOException ex) {
log.error("XMLReader结构时出现IO错误(/XmlConfig/Config.xml):" + ex.toString());
}
catch (JDOMException ex1) {
log.error("XMLReader结构时分析XML文件出错(/XmlConfig/Config.xml):" + ex1.toString());
}
catch (Exception ex) {
log.error("XMLReader结构出错(/XmlConfig/Config.xml):" + ex.toString());
}
}

/**
* <font color="orange">web使用发布在web服务器的绝对路径根目录,最后曾经有目录分割符。</font>
* @return <font color="tomato">前往web使用发布在web服务器的绝对路径的根目录。</font>
*/
public String getWebAppPa