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));