package util;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* XML文件操作类
*
*/
public class CDcwXmlFile {
private static Log logger=LogFactory.getLog(CDcwXmlFile.class);
/**
* 当前xml文档集
*/
private Document doc;
/**
* 当前操作点
*/
private Node root;
/**
* 构造函数
*
* @param xmlFileName
* XML文件路径
*/
public CDcwXmlFile(String xmlFileName)
{
super();
FileInputStream is=null;
try
{
is = new FileInputStream(xmlFileName);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
this.doc = builder.parse(is);
is.close();
this.root = this.doc.getFirstChild();
}
catch (Exception e)
{
this.doc = null;
this.root = null;
logger.info(e);
}finally{
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
logger.info(e);
}
}
}
/**
* 选择上一级结点为当前结点
*/
public boolean selectParent() {
if (this.root != null) {
Node node = root.getParentNode();
if (node != null) {
this.root = node;
return true;
}
}
return false;
}
/**
* 选择根结点
*/
public boolean selectRoot() {
if (this.doc != null) {
this.root = this.doc.getFirstChild();
if (this.root != null) {
return true;
}
}
return false;
}
/**
* 选择指定子节点为当前节点
*
* @param tagName
* 节点名
*/
public boolean selectSubItem(String tagName) {
try {
Element ele = (Element) this.root;
NodeList nodelist = ele.getElementsByTagName(tagName);
if ((nodelist != null) && (nodelist.getLength() > 0)) {
this.root = nodelist.item(0);
return true;
}
} catch (Exception e) {
logger.info(e);
}
return false;
}
/**
* 读取字符串值
*
* @param tagName
* 节点名
* @return 成功返字符串值,失败返回 ""
*/
public String readStr(String tagname) {
return readStr(this.root, tagname);
}
/**
* 读取字符串值
*
* @param node
* 当前值的父节点
* @param tagName
* 值节点名
* @return 成功返字符串值,失败返回 ""
*/
public String readStr(Node node, String tagname) {
String str = "";
if (null != node) {
try {
NodeList nl = ((Element) node).getElementsByTagName(tagname);
if ((nl != null) && (nl.getLength() > 0)) {
str = nl.item(0).getFirstChild().getNodeValue();
}
} catch (Exception e) {
logger.info(e);
}
}
return str;
}
/**
* 读取数字值
*
* @param tagName
* 节点名
* @param defaultvalue
* 默认值
* @return 成功返数字值,失败返回默认值
*/
public int readInt(String tagname, int defaultvalue) {
return readInt(this.root, tagname, defaultvalue);
}
/**
* 读取数字值
*
* @param node
* 当前值的父节点
* @param tagName
* 节点名
* @param defaultvalue
* 默认值
* @return 成功返数字值,失败返回默认值
*/
public int readInt(Node node, String tagname, int defaultvalue) {
String str = readStr(node, tagname);
if (str != "") {
try {
return Integer.parseInt(str);
} catch (Exception e) {
logger.info(e);
}
}
return defaultvalue;
}
}
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<!--日志-->
<log>
<!--########-->
<path>/HongKong/transit/logs/</path>
<!-- 记录日志级别,0-不记录 1-起动失败错误 2-警告错误 3-详细记录 4-程序日志记录 -->
<level>3</level>
<!--日志记录和的效天数-->
<validityday>4</validityday>
</log>
</xml-body>