package com.maomao.xpath.test;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.maomao.xpath.utils.XPathUtils;
public class PredicatesTest {
private static final String RES = "resource/predicatesTest.xml";
public static void main(String[] args) {
queryTheTitleOfFirstBook();
queryBookNameOfLastOne();
queryBookNameOfLastSecondOne();
queryBookNameOfBeforTwo();
queryBookNameOfWhosePriceMoreThanfifity();
queryNodeHasAttribute();
queryBookAttributeIsCN();
queryAllNode();
queryTitleAndPrice();
}
/**
* 获取第一本书的名字
*/
private static void queryTheTitleOfFirstBook() {
NodeList result = XPathUtils.queryNodelist(RES, "/bookstore/book[1]/title");
System.out.println("***********************查询第一本书的名称**************************");
printInfo(result);
}
/**
* 获取最后一本书的书名
*/
private static void queryBookNameOfLastSecondOne() {
NodeList result = XPathUtils.queryNodelist(RES, "/bookstore/book[last()]/title");
System.out.println("**********************查询最后一本书的名称*************************");
printInfo(result);
}
/**
* 获取倒数第二本书的名字
*/
private static void queryBookNameOfLastOne() {
NodeList result = XPathUtils.queryNodelist(RES, "/bookstore/book[last()-1]/title");
System.out.println("**********************查询倒数第二本书的名称*************************");
printInfo(result);
}
/**
* 查询前面两本书的书名
*
*/
private static void queryBookNameOfBeforTwo() {
NodeList result = XPathUtils.queryNodelist(RES, "/bookstore/book[position()<3]/title");
System.out.println("**********************查询前面两本书的书名*************************");
printInfo(result);
}
/**
* 查询价格大于50的书籍
*/
private static void queryBookNameOfWhosePriceMoreThanfifity() {
NodeList result = XPathUtils.queryNodelist(RES, "/bookstore/book[price>50.0]/title");
System.out.println("**********************查询价格大于50的书籍*************************");
printInfo(result);
}
/**
* 选取所有带有属性的 title 元素。
*/
private static void queryNodeHasAttribute() {
NodeList result = XPathUtils.queryNodelist(RES, "//title[@*]");
System.out.println("**********************选取所有带有属性的 title 元素*************************");
printInfo(result);
}
/**
* 选取所有中文书的价格
*/
private static void queryBookAttributeIsCN() {
NodeList result = XPathUtils.queryNodelist(RES, "/bookstore/book[title[@lang='cn']]/price");
System.out.println("**********************选取所有中文书的价格*************************");
printInfo(result);
}
/**
* 选取所有节点
*/
private static void queryAllNode() {
NodeList result = XPathUtils.queryNodelist(RES, "//*");
System.out.println("**********************选取所有文档节点*************************");
printInfo(result);
}
/**
* 选取所有书的书名和价格
*/
private static void queryTitleAndPrice() {
NodeList result = XPathUtils.queryNodelist(RES, "//book/title|//book/price");
System.out.println("**********************选取所有书的书名和价格*************************");
printInfo(result);
}
private static void printInfo(NodeList result) {
System.out.println("查询结果:");
System.out.println("查询到的 (" + result.getLength() + ") 个节点。");
for (int i = 0; i < result.getLength(); i++) {
Node node = result.item(i);
String nodeName = node.getNodeName();
String text = node.getTextContent();
System.out.println(nodeName + " --> " + text);
}
System.out.println("*****************************************************************\r\n\r\n");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
xpath.rar (17个子文件)
xpath
.project 381B
resource
node.xml 213B
predicatesTest.xml 598B
Axes.xml 240B
src
com
maomao
xpath
utils
XPathUtils.java 2KB
test
PredicatesTest.java 4KB
NodeTest.java 926B
AxesTest.java 1KB
.settings
org.eclipse.jdt.core.prefs 629B
.classpath 348B
bin
com
maomao
xpath
utils
XPathUtils.class 3KB
test
NodeTest.class 1KB
AxesTest.class 1KB
PredicatesTest.class 4KB
node.xml 213B
predicatesTest.xml 598B
Axes.xml 240B
共 17 条
- 1
资源评论
- 风去影留2013-12-30额,初学者看着有点晕。。。不过资源没问题~
- themoonsky2013-11-28xpath是一个很好用的api,GOOD
- 鬼芒果2014-05-08xpath查询的确很强大,很不错。。
- jiexinglongtao2014-08-15有个参考,问题解决了
- 俾德丽采2013-11-16xpath查询很强大,不错
scmky
- 粉丝: 1
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功