XPath详解
来源:互联网 发布:mysql myql 主从好处 编辑:程序博客网 时间:2024/05/17 07:45
- import java.io.IOException;
- import javax.xml.parsers.*;
- import javax.xml.xpath.*;
- import org.w3c.dom.*;
- import org.xml.sax.SAXException;
- public class XpathTest {
- public static void main(String[] args) throws ParserConfigurationException,
- SAXException, IOException, XPathExpressionException {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setNamespaceAware(false);
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse("C:/Users/Administrator/Desktop/test.xml");
- System.out.println(doc.getChildNodes().getLength());
- XPathFactory xFactory = XPathFactory.newInstance();
- XPath xpath = xFactory.newXPath();
- XPathExpression expr = xpath
- .compile("//name/text()");
- Object result = expr.evaluate(doc, XPathConstants.NODESET);
- NodeList nodes = (NodeList) result;
- System.out.println(nodes.getLength());
- for (int i = 0; i < nodes.getLength(); i++) {
- System.out.println(nodes.item(i).getNodeValue());
- }
- }
- }
一、结点类型
XPath中有七种结点类型:元素、属性、文本、命名空间、处理指令、注释以及文档节点(或成为根节点)。 文档的根节点即是文档结点;对应属性有属性结点,元素有元素结点。
二、常用路径表达式
例如有文档:
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <bookstore>
- <book>
- <title lang="eng">Harry Potter</title>
- <price>29.99</price>
- </book>
- <book>
- <title lang="eng">Learning XML</title>
- <price>39.95</price>
- </book>
- </bookstore>
则:
三、限定语
用来查找某个特定的节点或者包含某个指定的值的节点。以方括号括起。
例如:
四、通配符
| 选取若干路径
例如:
//book/title | //book/price选取所有 book 元素的 tilte 和 price 元素。//title | //price选取所有文档中的 title 和 price 元素。/bookstore/book/title | //price选取所有属于 bookstore 元素的 book 元素的 title 元素,以及文档中所有的 price 元素。
五、函数
绝对位置路径:
/step/step/...
相对位置路径:
step/step/...其中的每一步又可以是一个表达式,包括:
- 轴(函数)(axis)
- 定义所选节点与当前节点之间的树关系
- 节点测试(node-test)
- 识别某个轴内部的节点
- 零个或者更多谓语(predicate)
- 更深入地提炼所选的节点集
六、运算符
七、在Java中使用Xpath
在java1.5中推出了一个javax.xml.xpath包专门用来在java中使用Xpath表达式来读取xml。
1. 数据类型
在学习之前首先需要注意的是:Xpath的数据并不与Java有一一对应关系,Xpath1.0只声明了四种数据类型:
对应到java就是:
java.lang.Double
java.lang.String
java.lang.Boolean
org.w3c.dom.NodeList
因此,在使用java的xpathAPI时,需要注意返回类型:
Java代码- public Object evaluate(Object item, QName returnType)throws XPathExpressionException;
- public String evaluate(Object item)throws XPathExpressionException;
- public Object evaluate(InputSource source, QName returnType)throws XPathExpressionException;
- public String evaluate(InputSource source)throws XPathExpressionException;
- public Object evaluate(Object item, QName returnType)throws XPathExpressionException;
- public String evaluate(Object item)throws XPathExpressionException;
- public Object evaluate(InputSource source, QName returnType)throws XPathExpressionException;
- public String evaluate(InputSource source)throws XPathExpressionException;
不指定返回类型时,缺省返回类型为String。指定返回类型时,需要把返回值由Object类型强制转换成对应的返回类型。
2. API的使用
类似于Dom,要得到一个Xpath对象,可以如下使用: Java代码
- <strong><strong> XPathFactory factory = XPathFactory.newInstance();
- XPath xpath = factory.newXPath();
- XPathExpression expression = xpath.compile("/bookstore//book/title/text()");</strong></strong>
- <strong><strong> DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
- Document document = documentBuilder.parse(new File("books.xml"));
- NodeList list = (NodeList) expression.evaluate(document,XPathConstants.NODESET);</strong></strong>
最后,我们得到一个title的list值:
- <strong><strong> for(int i = 0;i</strong></strong>
- Everyday Italian
- Harry Potter
- XQuery Kick Start
- Learning XML
- <strong><strong>Everyday Italian
- Harry Potter
- XQuery Kick Start
- Learning XML</strong></strong>
八、处理命令空间
一般一个规范xml都会有命名空间的定义,例如:
- <strong><strong>
- Hello
- </strong></strong>
- <?xml version="1.0" encoding="UTF-8"?>
- <tg:bookstore xmlns:tg="http://www.tibco.com/cdc/liugang"
- xmlns:ns="http://www.tibco.com/cdc/liugang/ns">
- <ns:book>
- <tg:title>Hello</tg:title>
- </ns:book>
- </tg:bookstore>
xpath中定义了与节点名和命名空间有关的三个函数:
- local-name()
- namespace-uri()
- name()
- <strong><strong> XPathFactory xPathFactory = XPathFactory.newInstance();
- XPath xpath = xPathFactory.newXPath();
- XPathExpression compile = xpath.compile("//*[local-name()='book']");
- NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);</strong></strong>
- <strong><strong>
- Hello
- ElsIOIELdslke-1233
- </strong></strong>
- <strong><strong>class CustomNamespaceContext implements NamespaceContext{
- public String getNamespaceURI(String prefix) {
- if(prefix.equals("ns")){
- return "http://www.tibco.com/cdc/liugang/ns";
- }else if(prefix.equals("tg")){
- return "http://www.tibco.com/cdc/liugang/tg";
- }else if(prefix.equals("df")){
- return "http://www.tibco.com/cdc/liugang";
- }
- return XMLConstants.NULL_NS_URI;
- }
- public String getPrefix(String namespaceURI) {
- return null;
- }
- public Iterator getPrefixes(String namespaceURI) {
- return null;
- }
- }</strong></strong>
- <strong><strong> XPathFactory xPathFactory = XPathFactory.newInstance();
- XPath xpath = xPathFactory.newXPath();
- xpath.setNamespaceContext(new CustomNamespaceContext());
- XPathExpression compile = xpath.compile("//df:computer");
- NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);
- for(int i = 0;i</strong></strong>
九、其他
除此之外,在java中,还可以定义扩展的函数解释器和变量解释器,看XPath的方法:
- <strong><strong> /**
- * Establish a variable resolver.
- *
- * A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.
- *
- * @param resolver Variable resolver.
- *
- * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
- */
- public void setXPathVariableResolver(XPathVariableResolver resolver);
- /**
- * Establish a function resolver.
- *
- * A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.
- *
- * @param resolver XPath function resolver.
- *
- * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
- */
- public void setXPathFunctionResolver(XPathFunctionResolver resolver);</strong></strong>
0 0
- XPath详解
- XPath详解
- XPath详解
- XPath 详解
- Xpath 详解
- xpath详解
- XPath详解,总结
- XPath 详解,总结
- 果断收藏~xpath详解
- XPath语法详解
- iOS Xpath详解
- XPath 详解,总结
- XPath 详解,总结
- xpath 示例 详解
- XPath 详解,总结
- XPath实例详解
- Xpath基本用法详解
- XPath 详解,总结
- Visual Studio 2008项目中WinForm窗体图标显示为类图标,只能打开代码而无法打开视图问题解决
- Nginx Location 语法,与简单配置
- s
- YUI(Yahoo User Interface)介绍
- HttpClient之简单爬取页面的实现
- XPath详解
- latlng建立联合索引,利用范围空间检索
- Data Transformation Services (DTS)相关
- 正则表达式工作原理
- oracle创建dblink 语法
- ECMAScript5和ECMAScript6_浏览器支持情况
- 中国历史朝代顺序表
- WCF安全认证之UserName身份验证
- CMS中的回车体会成br