使用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 namespaceMap = new HashMap();
   
    public DefaultNamespaceContext(Map 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 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




  host
  http://192.168.3.249
  系统主机地址



  login
  /page/Default.aspx
  登陆页面

 

4. 用法

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

你可能感兴趣的:(XML,JDK,Java)