使用JDK自带类库操作XML三 Xpath

1. 环境

jdk1.6

2. 代码

public class XmlException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public XmlException(String message){
        super(message);
    }
   
    public XmlException(String message, Throwable cause){
        super(message, cause);
    }
}

 

import java.util.HashMap;

import java.util.Iterator;
import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

public class DefaultNamespaceContext implements NamespaceContext {
   
    private Map<String, String> namespaceMap = new HashMap<String, String>();
   
    public DefaultNamespaceContext(Map<String, String> namespaceMap){
        this.namespaceMap = namespaceMap;
    }

    public String getNamespaceURI(String prefix) {
        System.out.println("prefix = " + prefix);
        if (prefix == null)
            throw new NullPointerException("Null prefix");
        else if ("xml".equals(prefix))
            return XMLConstants.XML_NS_URI;
        else if(namespaceMap.containsKey(prefix)){
            return namespaceMap.get(prefix);
        }
        return XMLConstants.NULL_NS_URI;
    }

    public String getPrefix(String namespaceURI) {
        System.out.println("namespaceURI = " + namespaceURI);
        throw new UnsupportedOperationException();
    }

    public Iterator getPrefixes(String namespaceURI) {
        System.out.println("namespaceURI = " + namespaceURI);
        throw new UnsupportedOperationException();
    }

}

 

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;

/**
 * 实用XPATH解析xml的工具
 * xpath表达式用法: /图书馆/书/@名称 , /configuration/property[name = 'host']/value
 *
 * @author yuan
 *
 */
public class XpathTool {
   
    private String xmlFile;
    private String encoding;
   
    public XpathTool(final String xmlFile){
        this(xmlFile, "UTF-8");
    }
   
    public XpathTool(final String xmlFile, final String encoding){
        this.xmlFile = xmlFile;
        this.encoding = encoding;
    }
   
    public String compute(String expression, Map<String, String> namespaceMap) throws XmlException{
        String value = "";
        InputStreamReader isr = null;
        try {
            isr = new InputStreamReader(XpathTool.class.getResourceAsStream(xmlFile),encoding);
            InputSource source = new InputSource(isr);
            XPath xpath = XPathFactory.newInstance().newXPath();
           
            if(namespaceMap != null){
                xpath.setNamespaceContext(new DefaultNamespaceContext(namespaceMap));
                System.out.println("设置名称空间...");
            }
           
            value = xpath.evaluate(expression, source);
        }  catch (UnsupportedEncodingException e) {
            throw new XmlException(e.getMessage(), e);
        } catch (XPathExpressionException e) {
            throw new XmlException(e.getMessage(), e);
        } finally{
            if(isr!=null){
                try {
                    isr.close();
                } catch (IOException e) {
                    throw new XmlException(e.getMessage(), e);
                }
            }//if
        }
       
        return value;
    }
  
   
}

 

3. 使用的xml文件test.xml

<?xml version="1.0"?>

<configuration>

<property>
  <name a="1">host</name>
  <value>http://192.168.3.249</value>
  <description>系统主机地址</description>
</property>

<property>
  <name>login</name>
  <value>/page/Default.aspx</value>
  <description>登陆页面</description>
</property>

</configuration>

 

4. 用法

        XpathTool xpath = new XpathTool("d:/test.xml");
        System.out.println(xpath.compute("/configuration/property[name = 'host']/value", null));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值