XPath读取xml的工具类

1 篇文章 0 订阅
1 篇文章 0 订阅
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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值