java用xpath需要导入JsoupXpath-0.3.2.jar架包
解析html和xml
xpath语法变量
/ : 绝对路径
// : 相对路径
[] : 条件
@ : 指定属性(一般写于[]中或直接写在/后面表示将属性输出)
- : 通配符(匹配所有对象)
text(): 当前控件下的文本
JXDocument doc = new JXDocument("html/xml");
List<JXNode> list = doc.selN("xpath");
//JXNode可以.sel()继续搜索下去,JXNode其toString方法为输出其内容
List<Object> list = doc.sel("xpath");
https://www.cnblogs.com/wanghaomiao/p/4899355.html
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import java.util.List;
public class test {
public static void main(String[] args) throws XpathSyntaxErrorException {
JXDocument doc = new JXDocument(
"<?xml version="1.0" encoding="UTF-8"?>\n" +
"<apps>\n" +
"\t<app>\n" +
"\t\t<id>1</id>\n" +
"\t\t<name>Google</name>\n" +
"\t\t<version>1.0</version>\n" +
"\t</app>\n" +
"\t<app>\n" +
"\t\t<id>2</id>\n" +
"\t\t<name>Edge</name>\n" +
"\t\t<version>2.0</version>\n" +
"\t</app>\n" +
"</apps>"
);
List<JXNode> list = doc.selN("//app");
System.out.println("------------");
for (int i = 0; i < list.size(); i++) {
JXNode app = list.get(i);
System.out.println("id:"+app.sel("id/text()").get(0));
System.out.println("name:"+app.sel("name/text()").get(0));
System.out.println("version:"+app.sel("version/text()").get(0));
System.out.println("------------");
}
}
}
/*
运行结果:
------------
id:1
name:Google
version:1.0
------------
id:2
name:Edge
version:2.0
------------
*/
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import java.util.List;
public class test {
public static void main(String[] args) throws XpathSyntaxErrorException {
JXDocument doc = new JXDocument(
"<!DOCTYPE html>\n" +
"<html lang="en">\n" +
"<head>\n" +
" <meta charset="UTF-8">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>\n" +
" <div class="apps">\n" +
" <div class="app" style="color: red">\n" +
" <p2 class="id">1</p2>\n" +
" <p2 class="name">Google</p2>\n" +
" <p2 class="version">1.0</p2>\n" +
" </div>\n" +
" <div class="app" style="color: blue">\n" +
" <p2 class="id">2</p2>\n" +
" <p2 class="name">Edge</p2>\n" +
" <p2 class="version">2.1</p2>\n" +
" </div>\n" +
" </div>\n" +
"</body>\n" +
"</html>"
);
List<JXNode> list = doc.selN("//div[@class='app']");
System.out.println("------------");
for (int i = 0; i < list.size(); i++) {
JXNode app = list.get(i);
System.out.println("style"+app.sel("@style").get(0));
System.out.println("id:"+app.sel("p2[@class='id']/text()").get(0));
System.out.println("name:"+app.sel("p2[@class='name']/text()").get(0));
System.out.println("version:"+app.sel("p2[@class='version']/text()").get(0));
System.out.println("------------");
}
}
}
/*
运行结果:
------------
stylecolor: red
id:1
name:Google
version:1.0
------------
stylecolor: blue
id:2
name:Edge
version:2.1
------------
*/