import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
/**
* 为读XPath提供帮助<br>
* <br>
* <b><font color="red">注:XPath本质上采用dom方式解析xml,所以不适合解析大xml.</font></b>
* @author shanl
*/
public class XPathReader {
private String xml = null;
private static Logger logger = Logger.getLogger(XPathReader.class);
public XPathReader(String xml){
this.xml = xml;
}
public XPathReader(Document xmlDocument){
this.xmlDocument = xmlDocument;
}
/**
* 从一个xml加载数据
* @param xml xml字符串
*/
public static XPathReader createXmlPathReaderByXml(String xml){
XPathReader xmlReader = null;
try {
xmlReader = new XPathReader(xml);
} catch (Exception e) {
throw new RuntimeException(e);
}
return xmlReader;
}
/**
* 从一个XML文件中读取数据
* @param xmlFile xml文件
* @return
*/
public static XPathReader createXmlPathReaderByFile(File xmlFile){
XPathReader xmlReader = null;
Document xmlDocument = null;
try {
xmlDocument = new SAXReader().read(new FileInputStream(xmlFile));
xmlReader = new XPathReader(xmlDocument);
} catch (Exception e) {
throw new RuntimeException(e);
}
return xmlReader;
}
/**
* 从系统环境中查找一个xml文件,加载数据
* @param res xml文件资源
* @return
*/
public static XPathReader createXmlPathReaderByResource(String res){
XPathReader xmlReader = null;
Document xmlDocument = null;
URL url = ClassLoader.getSystemResource(res);
if(null == url){
throw new RuntimeException(res + " not found.");
}
try {
xmlDocument = new SAXReader().read(new FileInputStream(new File(url.toURI())));
xmlReader = new XPathReader(xmlDocument);
} catch (Exception e) {
throw new RuntimeException(e);
}
return xmlReader;
}
/**
* 返回xpath所表示的结点数
* @param xpath
* @return
*/
public int countNode(String xpath){
Document fileDocument = readDocument();
if(!findXPath(xpath)){
return 0;
}
List es = fileDocument.selectNodes(xpath);
if(null == es){
return 0;
}else{
return es.size();
}
}
/**
* 以xpath方式得到结果的文本
* @param xpath
* @return 如果不存在此结点,则返回""
*/
public String getText(String xpath){
String text = "";
Document fileDocument = readDocument();
if(!findXPath(xpath)){
return "";
}
Node node = fileDocument.selectSingleNode(xpath);
text = node.getText();
return text;
}
/**
* 以xpath方式得到结点某属性的值
* @param xpath
* @param attrName
* @return
*/
public String getAttribute(String xpath, String attrName){
String attr = "";
Document fileDocument = readDocument();
if(!findXPath(xpath)){
return "";
}
List es = fileDocument.selectNodes(xpath);
attr = ((Element)es.get(0)).attributeValue(attrName);
return attr;
}
/**
* 查找xpath是否存在
* @param xpath
* @return 存在返回true,否则返回false;
*/
public boolean findXPath(String xpath){
Document fileDocument = readDocument();
try{
List es = fileDocument.selectNodes(xpath);
if(0 == es.size()){
return false;
}else{
return true;
}
}catch(Exception ex){
return false;
}
}
/**
* 读取配置文件
*
* @return 返回Document对象
*/
private Document readDocument() {
SAXReader reader = new SAXReader();
// Document fileDocument = null;
try {
if(null == xmlDocument){
xmlDocument = reader.read(new StringReader(xml));
}
// if(cache){
// if(null == fileDocument){
// fileDocument = reader.read(new FileInputStream(file));
// }
// }else{
// fileDocument = reader.read(new FileInputStream(file));
// }
} catch(Exception ex){
logger.error(ex.getMessage(), ex);
}
return xmlDocument;
}
private Document xmlDocument = null;
}
需要jaxen-1.1-beta-7.jar、dom4j-1.6.1.jar、log4j.jar 或更高的jar