如何实现一个读取xml文件内容的类
这篇文章将为大家详细讲解有关如何实现一个读取xml文件内容的类,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
一个读取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.*;
public class XMLReader {
static public Logger log = Logger.getLogger(XMLReader.class);
protected Element m_RootElement = null;
protected String m_webAppPath = null;
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(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());
}
}
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( 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());
}
}
public String getWebAppPath() {
return m_webAppPath;
}
public String getElementvalue(String curRootName, String key) {
String value = null;
Element curRoot = getElement(null, curRootName);
if (null == curRoot) {
curRoot = m_RootElement;
}
Element keyNode = getElement(curRoot, key);
if (null != keyNode) {
value = keyNode.getTextTrim();
}
return value;
}
private Element getElement(Element curRoot, String nodeName) {
Element retElement = null;
if (null == nodeName)
return m_RootElement;
if (null == curRoot) {
curRoot = m_RootElement;
}
if (null != curRoot) {
retElement = curRoot.getChild(nodeName);
if (null == retElement) {
List nestElements = curRoot.getChildren();
Iterator iterator = nestElements.iterator();
while (iterator.hasNext() && null == retElement) {
retElement = getElement( (Element) iterator.next(), nodeName);
}
}
}
return retElement;
}
public String getElementAtrribute(String elementName, String attName)
{
Element el = getElement(null, elementName);
if (null == el)
return null;
return el.getAttributevalue(attName);
}
}
关于“如何实现一个读取xml文件内容的类”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341