81,094
社区成员
发帖
与我相关
我的任务
分享
package org.sl.xml;
import java.io.File;
import java.io.FileInputStream;
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方式读取xml
* @author Shanl
*
*/
public class XPathHelper {
private File file = null;
private boolean cache = false;
private static Logger logger = Logger.getLogger(XPathHelper.class);
/**
* 构造器<br>
* 相当于XPathHelper(res, true);
* @param res 资源名
*/
public XPathHelper(String res){
this(res, true);
}
/**
* 构造器<br>
* 注:这个方法会在系统中查找与res匹配的资源,如果找不到则抛出RunntimeException
* @param res 资源名
* @param cahce 是否缓存XML
*/
public XPathHelper(String res, boolean cache){
this.cache = cache;
URL url = ClassLoader.getSystemResource(res);
if(null == url){
throw new RuntimeException(res + " not found.");
}
String protocol = url.getProtocol();
String path = url.toString();
path = path.replaceFirst(protocol + ":/", "");
file = new File(path);
if(!file.exists()){
throw new RuntimeException(res + " not found.");
}
}
/**
* 返回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(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 fileDocument;
}
private Document fileDocument = null;
}
package test;
import org.apache.log4j.Logger;
import org.sl.xml.XPathHelper;
public class XPathHelperTest {
static Logger logger = Logger.getLogger(XPathHelperTest.class);
public static void main(String[] args){
if(true) t1();
}
static void t1(){
XPathHelper xpHelper = new XPathHelper("test1.xml");
int itemCount = xpHelper.countNode("//root/data[@count='5']/item");
for(int i = 1, end = itemCount; i < end; i++){
logger.info("create:" + xpHelper.getAttribute("//root/data[@count='5']/item["+i+"]", "create"));
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node id="node1">
<![CDATA[<node>123456</node>]]>
</node>
<code>1000</code>
<data count="5">
<item msgid="2070105" from="ID1" create="2011-03-14 10:55:56"
content="444444444" />
<item msgid="2070106" from="ID2" create="2011-03-14 10:58:51"
content="333333333" />
<item msgid="2070107" from="ID3" create="2011-03-14 11:01:26"
content="222222222" />
<item msgid="2070108" from="ID4" create="2011-03-16 14:02:13"
content="111111111" />
</data>
</root>