Java 5 推出了 javax.xml.xpath 包,這是一個用於 XPath 文檔查詢的獨立於 XML 對象模型的庫。
強大的xpath表達式支持對xml document文檔檢索信息。
<?xml version="1.0" encoding="UTF-8"?> <inventory> <book year="2000"> <title>Snow Crash</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553380958</isbn> <price>14.95</price> </book> <book year="2005"> <title>Burning Tower</title> <author>Larry Niven</author> <author>Jerry Pournelle</author> <publisher>Pocket</publisher> <isbn>0743416910</isbn> <price>5.99</price> </book> <book year="1995"> <title>Zodiac</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553573862</isbn> <price>7.50</price> </book> </inventory>
import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class XPathExample {
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
//創建 XPathFactory:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("book.xml");
XPathFactory factory = XPathFactory.newInstance();
//這個工廠創建 XPath 對象:
XPath xpath = factory.newXPath();
//設置命名空間
// xpath.setNamespaceContext(new PersonalNamespaceContext());
//XPath 對象編譯 XPath 表達式:
XPathExpression expr = xpath.compile("//book[author='Neal Stephenson']/title/text()");
//doc是整個節點
/**
* XPathConstants.NODESET
XPathConstants.BOOLEAN
XPathConstants.NUMBER
XPathConstants.STRING
XPathConstants.NODE
定義了返回類型
xpath 和java的映射關係
number 映射爲 java.lang.Double
string 映射爲 java.lang.String
boolean 映射爲 java.lang.Boolean
node-set 映射爲 org.w3c.dom.NodeList
*/
Object result = expr.evaluate(doc, XPathConstants.NODESET);
//類型轉換
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
}
xpath 表達式代碼
XPathExpression expr = xpath.compile("//book[author='Neal Stephenson']/title/text()");
//book[author='Neal Stephenson'book標籤 下的text爲Neal Stephenson的author標籤
/title/test()是title 標籤的文本
很簡單吧,再說一下
實體解析器SAX EntityResolver
這裏有它詳細的解釋:http://www.ibm.com/developerworks/cn/xml/tips/x-tipent/
讓xml文檔包含外部實體引用
package com.ibm.developerWorks; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class CopyrightResolver implements EntityResolver { public InputSource resolveEntity(String publicID, String systemID) throws SAXException { if (systemID.equals("http://www.ibm.com/developerworks/copyright.xml")) { // Return local copy of the copyright.xml file return new InputSource("/usr/local/content/localCopyright.xml"); } // If no match, returning null makes process continue normally return null; }
再你的xml解析類中每次解析都會調用這個方法,這樣就避免了每次都要加載外部資源的消耗。
xpase的公用類:
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.apache.ibatis.builder.BuilderException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class XPathParser {
private Document document;//dom對象
private boolean validation;//是否驗證 domFactory.setNamespaceAware(validation);
private EntityResolver entityResolver;//實體解析器
private Properties variables;//配置文件
private XPath xpath;//xpath
public XPathParser(String xml) {
commonConstructor(false, null, null);//初始化XPath對象
this.document = createDocument(new InputSource(new StringReader(xml)));//獲取dom對象
}
public XPathParser(Reader reader) {
commonConstructor(false, null, null);
this.document = createDocument(new InputSource(reader));
}
public XPathParser(InputStream inputStream) {
commonConstructor(false, null, null);
this.document = createDocument(new InputSource(inputStream));
}
public XPathParser(Document document) {
commonConstructor(false, null, null);
this.document = document;
}
public XPathParser(String xml, boolean validation) {
commonConstructor(validation, null, null);
this.document = createDocument(new InputSource(new StringReader(xml)));
}
public XPathParser(Reader reader, boolean validation) {
commonConstructor(validation, null, null);
this.document = createDocument(new InputSource(reader));
}
public XPathParser(InputStream inputStream, boolean validation) {
commonConstructor(validation, null, null);
this.document = createDocument(new InputSource(inputStream));
}
public XPathParser(Document document, boolean validation) {
commonConstructor(validation, null, null);
this.document = document;
}
public XPathParser(String xml, boolean validation, Properties variables) {
commonConstructor(validation, variables, null);
this.document = createDocument(new InputSource(new StringReader(xml)));
}
public XPathParser(Reader reader, boolean validation, Properties variables) {
commonConstructor(validation, variables, null);
this.document = createDocument(new InputSource(reader));
}
public XPathParser(InputStream inputStream, boolean validation, Properties variables) {
commonConstructor(validation, variables, null);
this.document = createDocument(new InputSource(inputStream));
}
public XPathParser(Document document, boolean validation, Properties variables) {
commonConstructor(validation, variables, null);
this.document = document;
}
public XPathParser(String xml, boolean validation, Properties variables, EntityResolver entityResolver) {
commonConstructor(validation, variables, entityResolver);
this.document = createDocument(new InputSource(new StringReader(xml)));
}
public XPathParser(Reader reader, boolean validation, Properties variables, EntityResolver entityResolver) {
commonConstructor(validation, variables, entityResolver);
this.document = createDocument(new InputSource(reader));
}
public XPathParser(InputStream inputStream, boolean validation, Properties variables, EntityResolver entityResolver) {
commonConstructor(validation, variables, entityResolver);
this.document = createDocument(new InputSource(inputStream));
}
public XPathParser(Document document, boolean validation, Properties variables, EntityResolver entityResolver) {
commonConstructor(validation, variables, entityResolver);
this.document = document;
}
public void setVariables(Properties variables) {
this.variables = variables;
}
public String evalString(String expression) {
return evalString(document, expression);
}
public String evalString(Object root, String expression) {
String result = (String) evaluate(expression, root, XPathConstants.STRING);
result = PropertyParser.parse(result, variables);
return result;
}
public Boolean evalBoolean(String expression) {
return evalBoolean(document, expression);
}
public Boolean evalBoolean(Object root, String expression) {
return (Boolean) evaluate(expression, root, XPathConstants.BOOLEAN);
}
public Short evalShort(String expression) {
return evalShort(document, expression);
}
public Short evalShort(Object root, String expression) {
return Short.valueOf(evalString(root, expression));
}
public Integer evalInteger(String expression) {
return evalInteger(document, expression);
}
public Integer evalInteger(Object root, String expression) {
return Integer.valueOf(evalString(root, expression));
}
public Long evalLong(String expression) {
return evalLong(document, expression);
}
public Long evalLong(Object root, String expression) {
return Long.valueOf(evalString(root, expression));
}
public Float evalFloat(String expression) {
return evalFloat(document, expression);
}
public Float evalFloat(Object root, String expression) {
return Float.valueOf(evalString(root, expression));
}
public Double evalDouble(String expression) {
return evalDouble(document, expression);
}
public Double evalDouble(Object root, String expression) {
return (Double) evaluate(expression, root, XPathConstants.NUMBER);
}
public List<XNode> evalNodes(String expression) {
return evalNodes(document, expression);
}
public List<XNode> evalNodes(Object root, String expression) {
List<XNode> xnodes = new ArrayList<XNode>();
NodeList nodes = (NodeList) evaluate(expression, root, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
xnodes.add(new XNode(this, nodes.item(i), variables));
}
return xnodes;
}
public XNode evalNode(String expression) {
return evalNode(document, expression);
}
public XNode evalNode(Object root, String expression) {
Node node = (Node) evaluate(expression, root, XPathConstants.NODE);
if (node == null) {
return null;
}
return new XNode(this, node, variables);
}
private Object evaluate(String expression, Object root, QName returnType) {
try {
return xpath.evaluate(expression, root, returnType);
} catch (Exception e) {
throw new BuilderException("Error evaluating XPath. Cause: " + e, e);
}
}
private Document createDocument(InputSource inputSource) {
// important: this must only be called AFTER common constructor
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validation);
factory.setNamespaceAware(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setCoalescing(false);
factory.setExpandEntityReferences(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(entityResolver);//設置實體解析器
builder.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException exception) throws SAXException {
throw exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
public void warning(SAXParseException exception) throws SAXException {
}
});
return builder.parse(inputSource);
} catch (Exception e) {
throw new BuilderException("Error creating document instance. Cause: " + e, e);
}
}
private void commonConstructor(boolean validation, Properties variables, EntityResolver entityResolver) {
this.validation = validation;
this.entityResolver = entityResolver;
this.variables = variables;
XPathFactory factory = XPathFactory.newInstance();
this.xpath = factory.newXPath();
}
}
這裏用到了一個字符串解析器:
PropertyParser 它是來處理字符串中${}這樣的字符,去配置文件中查找。
解析器模式:
import java.util.Properties;
public class PropertyParser {
public static String parse(String string, Properties variables) {
VariableTokenHandler handler = new VariableTokenHandler(variables);
GenericTokenParser parser = new GenericTokenParser("${", "}", handler);
return parser.parse(string);
}
private static class VariableTokenHandler implements TokenHandler {
private Properties variables;
public VariableTokenHandler(Properties variables) {
this.variables = variables;
}
public String handleToken(String content) {
if (variables != null && variables.containsKey(content)) {
return variables.getProperty(content);
}
return "${" + content + "}";
}
}
}
GenericTokenParser.java
public class GenericTokenParser {
private final String openToken;
private final String closeToken;
private final TokenHandler handler;
public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
this.openToken = openToken;
this.closeToken = closeToken;
this.handler = handler;
}
public String parse(String text) {
StringBuilder builder = new StringBuilder();
if (text != null && text.length() > 0) {//如果傳入的字符串有值
//將字符串轉爲字符數組
char[] src = text.toCharArray();
int offset = 0;
//判斷openToken在text中的位置,注意indexOf函數的返回值-1表示不存在,0表示在在開頭的位置
int start = text.indexOf(openToken, offset);
while (start > -1) {
if (start > 0 && src[start - 1] == '\\') {
//如果text中在openToken前存在轉義符就將轉義符去掉。如果openToken前存在轉義符,start的值必然大於0,最小也爲1
//因爲此時openToken是不需要進行處理的,所以也不需要處理endToken。接着查找下一個openToken
builder.append(src, offset, start - 1).append(openToken);
offset = start + openToken.length();//重設offset
} else {
int end = text.indexOf(closeToken, start);
if (end == -1) {//如果不存在openToken,則直接將offset位置後的字符添加到builder中
builder.append(src, offset, src.length - offset);
offset = src.length;//重設offset
} else {
builder.append(src, offset, start - offset);//添加openToken前offset後位置的字符到bulider中
offset = start + openToken.length();//重設offset
String content = new String(src, offset, end - offset);//獲取openToken和endToken位置間的字符串
builder.append(handler.handleToken(content));//調用handler進行處理
offset = end + closeToken.length();//重設offset
}
}
start = text.indexOf(openToken, offset);//開始下一個循環
}
//只有當text中不存在openToken且text.length大於0時纔會執行下面的語句
if (offset < src.length) {
builder.append(src, offset, src.length - offset);
}
}
return builder.toString();
}
}
簡單的說,這個函數的作用就是將openToken和endToken間的字符串取出來用handler處理下,然後再拼接到一塊。我們接下來看一個具體的handler,瞭解下它對傳入的字符串做了怎樣的處理。
在XMLConfigBuilder這個類中有它的調用實例:
//很關鍵是xpath的表達式語言 匹配對象 放入configuration中
private void parseConfiguration(XNode root) {
try {
propertiesElement(root.evalNode("properties")); //issue #117 read properties first
typeAliasesElement(root.evalNode("typeAliases"));
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
settingsElement(root.evalNode("settings"));
environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
這裏使用瞭解析器設計模式,這裏的文件可以單獨作爲我們以後對xml文件解析的使用。