package com.aptech.ServletMVCText.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import sun.security.jca.JCAUtil;
/**
*
* 一句话功能简述:添加ibatis配置文件
* 功能详细描述:〈功能详细描述〉
* @author 鲁文辉
* @e-mail 286352250@163.com
* @version [版本号, 2009-6-20 下午07:55:11]
* @see [相关类/方法]
* @since [产品/模块版本]
*
*/
public class XMLUtil {
private static XMLUtil xmlutil=null;
private FileInputStream fileIn=null;
private static JCAUtil jdomUtil=null;
private Document document=null;
//添加文件路径
public XMLUtil(String path) {
try {
fileIn = new FileInputStream(new File(path));
SAXBuilder sax = new SAXBuilder();
document = sax.build(fileIn);
fileIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
}
//单例
public static XMLUtil getInstance(String path){
if (xmlutil==null) {
xmlutil=new XMLUtil(path);
}
return xmlutil;
}
//递归
private Element getAllElemen(String key,Element element,String forward){
if(element==null){
element=document.getRootElement();
}
List<Element> list=element.getChildren();
for(Element temp : list){
if(temp.getAttributeValue("key").equals(forward)){
break;
}
else if( temp.getAttributeValue("key").equals(key)){
element = temp;
getAllElemen(key,element,forward);
}
}
return element;
}
//查询root节点下所有子节点
public Element getChildElement(String key){
Element childElement = null;
Element root = document.getRootElement();
List<Element> clild = root.getChildren();
for (Element element : clild) {
if( element.getAttributeValue("key").equals(key)){
childElement = element;
break;
}
}
return childElement;
}
//
public Element getGrandson(String key,String forward){
Element grandElement = null;
Element childElement = getChildElement(key);
List<Element> grandson = childElement.getChildren();
for (Element element : grandson) {
if( element.getAttributeValue("key").equals(forward)){
grandElement = element;
}
}
return grandElement;
}
public String getValue(String key){
Element childElement = getChildElement(key);
String value = childElement.getAttributeValue("value");
return value;
}
public String getForward(String key,String forward){
Element grandElement = getGrandson(key,forward);
// Element grandElement = getAllElemen(key,null,forward);
String value=grandElement.getAttributeValue("value");
return value;
}
}