import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/***
*
* @author yuanshishi
* 2017-02-25 读取xml内容
* 配置节点 对业务是否进行开放
* xmlName 传一个xml名称可以带.xml或者不带
* xmlFlag 传一个配置的节点名称
* 本次项目中 读取节点内容 1表是业务开放 0表示业务关闭
*
* cName_Value 为返回值(1、0、NotFindNode) 如果 为null或者为"" 时表示没有配置xml或者没有xml的节点(NotFindNode 表是 未配置节点等)
* 配置节点名称找不到或者节点内容为空时,方法返回NotFindNode
*/
public class XMLUtil{
public static String cName_Value="";//获取配置的xml标签 对业务是否开放
/**
* yuanshishi
* 2017-02-26 15:09 周日
* @param xmlName 表示xml的名字 如xxxx.xml
* @param xmlFlag 表示xml的标签 如<root>等等
* @return cName_Value 返回标签内容
* 将xxx.xml放到与webRoot同一级目录下面 不是webRoot下面
*/
public String ByDoMReadXML(String xmlName,String xmlFlag){
try {
//创建DOM文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
if((xmlName.trim()).indexOf(".xml")!=-1){
System.out.println("--------获取当前的配置文件为:"+xmlName+";>>>>>>>>>>>获取当前的节点名为:"+xmlFlag);
}else{
xmlName=xmlName+".xml";
System.out.println("******获取当前的配置文件为:"+xmlName+";*****>>>>>>>>>>>获取当前的节点名为:"+xmlFlag);
}
doc = builder.parse(new File(xmlName));//放到与webRoot同一级目录下面 不是webRoot下面
//获取包含类名的文本节点
NodeList nl = doc.getElementsByTagName(xmlFlag.trim());//获取节点名称
Node classNole = nl.item(0).getFirstChild();//获取根节点
cName_Value = classNole.getNodeValue()==null?"":classNole.getNodeValue();//获取节点值
System.out.println("=======获取xml标签返回的值为cName_Value========"+cName_Value);
return cName_Value;
} catch (Exception e) {
//配置节点名称找不到或者节点内容为空时
cName_Value="NotFindNode";
System.out.println("cName_Value===="+cName_Value);
return cName_Value;
}
}
/**
*
* yuanshishi
* 2017-02-26 15:30 周日
* @param xmlName 表示xml的名字 如xxxx.xml
* @param xmlNodeFlag 表示xml的标签 如<root>等等
* @return cName_Value 返回标签内容
* 将xxx.xml放在src类路径下面
*/
public String getXmlNodeValue(String xmlName,String xmlNodeFlag){
try {
// 解析config.xml配置文件 ,返回文件存储路径
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
if((xmlName.trim()).indexOf(".xml")!=-1){
System.out.println("--------获取当前的配置文件为:"+xmlName+";>>>>>>>>>>>获取当前的节点名为:"+xmlNodeFlag);
}else{
xmlName=xmlName+".xml";
System.out.println("******获取当前的配置文件为:"+xmlName+";*****>>>>>>>>>>>获取当前的节点名为:"+xmlNodeFlag);
}
String configPath = this.getClass().getResource("/").getPath() + File.separator+xmlName;
System.out.println("----------配置文件存在的路径configPath===="+configPath);
Document document = builder.parse(configPath);
NodeList nodeList = document.getElementsByTagName(xmlNodeFlag);
Node node = nodeList.item(0);
cName_Value = node.getTextContent();
System.out.println("=======获取xml标签返回的值为cName_Value========"+cName_Value);
return cName_Value;
} catch (ParserConfigurationException e) {
//配置节点名称找不到或者节点内容为空时
cName_Value="NotFindNode";
System.out.println("----------cName_Value===="+cName_Value);
return cName_Value;
} catch (SAXException e) {
//配置节点名称找不到或者节点内容为空时
cName_Value="NotFindNode";
System.out.println("***********cName_Value===="+cName_Value);
return cName_Value;
} catch (IOException e) {
//配置节点名称找不到或者节点内容为空时
cName_Value="NotFindNode";
System.out.println("#######cName_Value===="+cName_Value);
return cName_Value;
}catch (NullPointerException e){
//配置节点名称找不到或者节点内容为空时
cName_Value="NotFindNode";
System.out.println("#######cName_Value===="+cName_Value);
return cName_Value;
}
}
/**
* 2017-02-26 15:33
* @return bean对象
*/
public static Object getBean(){
try {
//创建DOM文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("config.xml"));//放到与webRoot同一级目录下面 不是webRoot下面
//doc = builder.parse("config.xml");
//获取包含类名的文本节点
NodeList nl = doc.getElementsByTagName("className");//获取节点名称
Node classNole = nl.item(0).getFirstChild();//获取根节点
String cName = classNole.getNodeValue();//获取节点值
//通过java反射机制 类名生成实例对象并将其返回
Class c = Class.forName(cName);
Object obj = c.newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取系统时间
* @return
*/
public static String getdate(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String dateTime=df.format(new Date());
return dateTime;
}
}