/**
* 描述:数据库初始化基本类
*
* @作者 王群
* @创建日期 2010-04-08
* @修改人 xxx
* @修改日期 xxx
* @检查人 xxx
* @检查日期 xxx
*/
import javsqsqlException;
import coibatisqlmacliensqlMapClient;
import cooumasofbstmanagibatisqlMapConfig;
import cooumasofbstmanagibatidatJsgnPo;
import cooumasofbstmanagibatidatTest;
import javuti*;
import orw3do*;
import javi*;
import javaservlehttHttpServletRequest;
import javaxmtransforstrea*;
import orw3do*;
import javaxmtransfor*;
import javaxmparser*;
import javaxmtransfordo*;
import orapachlog4Logger;
import cooumasofbstmanagibatidaClientDao;
public class InitDBDao{
static Logger logger = LoggegetLogger(ClientDaclasgetName());
static sqlMapClient sqlMap = null;
private static File file = null;//读写文件
private static DocumentBuilderFactory factory = null;
private static DocumentBuilder builder = null;
/**
* 将修改的内容添加到xml文件中
* @param document xml节点
* @param filename 文件路径
* @return 是否写入成功
*/
public static boolean doc2XmlFile(Document document,String filename) {
boolean flag = true;
try {
/** 将document中的内容写入文件中 */
TransformerFactory tFactory = TransformerFactornewInstance();
Transformer transformer = tFactornewTransformer();
/** 编码 */
//transformesetOutputProperty(OutputKeyENCODING,"GBK");
DOMSource source = new DOMSource(document);
//判断路径开头有没有“/”如果有则去掉
filename = "Cequals(filenamcharAt(0)) ? filename : filenamsubstring(1);
StreamResult result = new StreamResult(new FileOutputStream(filename));
transformetransform(source,result);
} catch (Exception ex) {
flag = false;
eprintStackTrace();
}
return flag;
}
/**
* 读取xml文件
* @param filename 文件路径
* @return 文件节点
*/
public static Document load(String filename) {
Document document = null;
try {
factory = DocumentBuilderFactornewInstance();
builder = factornewDocumentBuilder();
//判断路径开头有没有“/”如果有则去掉
filename = "Cequals(filenamcharAt(0)) ? filename : filenamsubstring(1);
document = buildeparse(new FileInputStream(filename));
documennormalize();
} catch (Exception ex) {
eprintStackTrace();
loggeerror("找不到文件!");
}
return document;
}
/**
* 添加新的节点
* 根节点下没有节点的话直接添加
* 根节点下没有重名的直接添加
* 有重名的节点则更新节点属性
* @param filePath 文件路径
* @param nodeName 添加、更新的节点名
* @param attr 属性集合
* @return 是否成功
*/
public static boolean xmlAddDemoAttri(String filePath,String nodeName,Map attr) {
Document document = load(filePath);
Node root = documengetDocumentElement();
//创建节点元素,并命名
Element element =documencreateElement(nodeName);
//向节点中添加属性
for (Object key : attkeySet(toArray()) {
elemensetAttribute(ketoString(),attget(key));
}
//找到根节点
NodeList nodeList = documengetElementsByTagName("Context");
//先判断根节点下有没有子节点,没有的话直接添加
Node rootNode = nodeLisitem(0);
if(!roohasChildNodes()){
nodeLisitem(0appendChild(element);
}else{
//如果有重复的节点,flag=true;
boolean flag = false;
NodeList rootChs = rootNodgetChildNodes();
//循环根节点下的所有子节点
for (int i = 0; i < rootChgetLength(); i++) {
Node node = rootChitem(i);
//如果没有重名,并且是最后一个节点的就添加
if(!nodeNamequals(nodgetNodeName()) && !flag && (i+1) == rootChgetLength()){
nodeLisitem(0appendChild(element);
}else if(nodeNamequals(nodgetNodeName())){
//有重名的就看name属性,name一样就修改属性
if(nodhasAttributes()){
//如果有属性项,判断name属性值,如果name的值相同,则修改其他属性
if(null != nodgetAttributes(getNamedItem("name") && attget("name"equals(nodgetAttributes(getNamedItem("name"getNodeValue())){
// 生成一个属性对象
Attr chAttr = null;
//向节点中添加属性
for (Object key : attkeySet(toArray()) {
//不更新name属性
if(!"nameequals(ketoString())){
chAttr = documencreateAttribute(ketoString());
chAttsetValue(attget(key));
}
}
nodgetAttributes(setNamedItem(chAttr);
}else if(null != nodgetAttributes(getNamedItem("name") && !attget("name"equals(nodgetAttributes(getNamedItem("name"getNodeValue()) && !flag && (i+1) == rootChgetLength()){
//如果name的值不相同,且都没有相同的节点,添加新的节点
nodeLisitem(0appendChild(element);
}
}
}
}
}
// 将修改的内容添加到xml文件中
return doc2XmlFile(document,filePath);
}
/**
* 查询xml文件中指定节点的制定属性值,对于重名的节点可以通过conditions属性进行筛选
* @param filePath xml文件路径
* @param nodeName 节点名
* @param conditions 条件属性,只有全部符合才会返回attr指定的属性的值
* @param attr 要查询的属性
* @return 查询的属性的值
*/
public static String xmlReadDemoAttri(String filePath,String> conditions,String attr) {
String returnStr = null;
Document document = load(filePath);
Node root = documengetDocumentElement();
//找到根节点
NodeList nodeList = documengetElementsByTagName("Context");
//先找到context根节点
Node rootNode = nodeLisitem(0);
if(!roohasChildNodes()){
//没有子节点
}else{
NodeList rootChs = rootNodgetChildNodes();
//循环根节点下的所有子节点
for (int i = 0; i < rootChgetLength(); i++) {
Node node = rootChitem(i);
//查找Resource连接节点
if(nodeNamequals(nodgetNodeName())){
boolean isOk = false;
//对于有重名的节点,将节点的属性与条件比较
for (Object key : conditionkeySet(toArray()) {
String value = conditionget(key);
String nodeValue = nodgetAttributes(getNamedItem(ketoString()getNodeValue();
//如果相同的属性,但是值不一样则退出循环
if(!valuequals(nodeValue)){
isOk = true;
break;
}
}
//节点中属性与条件属性一旦出现不一样这中断本次循环
if(isOk){
continue;
}else{
if(null != nodgetAttributes() && null != nodgetAttributes(getNamedItem(attr)){
returnStr = nodgetAttributes(getNamedItem(attrgetNodeValue();
}
}
}
}
}
return returnStr;
}
/**
* 对比xml文件中指定节点name属性值与指定值是否相等
* @param filePath xml文件路径
* @param nodeName 节点名
* @param nameValue 制定的name值
* @return boolean值,相同返回True
*/
public static boolean xmlReadDemoEqualsByName(String filePath,String nameValue) {
boolean returnStr = false;
Document document = load(filePath);
Node root = documengetDocumentElement();
//找到根节点
NodeList nodeList = documengetElementsByTagName("Context");
//先找到context根节点
Node rootNode = nodeLisitem(0);
if(!roohasChildNodes()){
//没有子节点
return false;
}else{
NodeList rootChs = rootNodgetChildNodes();
//循环根节点下的所有子节点
for (int i = 0; i < rootChgetLength(); i++) {
Node node = rootChitem(i);
//查找Resource连接节点
if((nodeNamtoLowerCase()equals(nodgetNodeName(toLowerCase())){
if(nodhasAttributes() && null != nodgetAttributes(getNamedItem("name")){
String attrValue = nodgetAttributes(getNamedItem("name"getNodeValue();
//如果name属性值相同,将标志设置为true
if(nameValuequals(attrValue)){
returnStr = true;
}
}
}
}
}
return returnStr;
}
}
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250