DOM:将标记语言文档一次性加载进内存,在内存中形成一颗dom树
优点:
操作方便,可以对文档进行CRUD(增删改查)的所有操作
缺点:
占内存
SAX:逐行读取,基于事件驱动
优点
不占内存
缺点
只能读取
常用的解析器:
快速入门
从URL,文件或字符串中刮取并解析HTML
查找和提取数据,使用DOM遍历或CSS选择器
操纵HTML元素,属性和文本
根据安全的白名单清理用户提交的内容,以防止XSS攻击
输出整洁的HTML
步骤:
代码:
xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<students>
<student number="heima_0001">
<name id="cat">tom</name>
<age>18</age>
<sex>male</sex>
</student>
<student number="heima_0002">
<name>jack</name>
<age>12</age>
<sex>male</sex>
</student>
</students>
测试代码:
public class JsoupTest {
public static void main(String[] args) throws IOException {
//获得路径path
String path = JsoupTest.class.getClassLoader().getResource("student.xml").getPath();
//解析
Document document = Jsoup.parse(new File(path), "utf-8");
//获取元素
Elements elements = document.getElementsByTag("name");
System.out.println(elements.size());
//获取数据
for (int i = 0; i < elements.size(); i++) {
System.out.println(elements.get(i).text());
}
}
}
对象的使用
Jsoup:工具类,可以解析html或xml文档,返回Document
parse方法
解析xml或html对象
public static Document parse(File in,String charsetName)throws IOException
Parse the contents of a file as HTML. The location of the file is used as the base URI to qualify relative URLs.
解析xml或html字符串
public static Document parse(String html)
Parse HTML into a Document. As no base URI is specified, absolute URL detection relies on the HTML including a
<base href>
tag.
通过网络路径获取指定的html或xml的文档对象
public static Document parse(URL url,int timeoutMillis)throws IOException
Fetch a URL, and parse it as HTML. Provided for compatibility; in most cases use
connect(String)
The encoding character set is determined by the content-type header or http-equiv meta tag, or falls back to
UTF-8
.
等;
Document:文档对象。代表内存中的dom树
获取Element对象
public Elements getElementsByTag(String tagName)
Finds elements, including and recursively under this element, with the specified tag name.
public Elements getElementsByAttribute(String key)
Find elements that have a named attribute set. Case insensitive.
public Elements getElementsByAttributeValue(String key, String value)
Find elements that have an attribute with the specific value. Case insensitive.
public Element lastElementSibling()
Gets the last element sibling of this element
Elements:Element对象的集合。可以当作ArrayList
Element:元素对象
Node:节点对象
selector:选择器
使用的方法:Elements select(String cssQuery)
样例:
public class JsoupTest {
public static void main(String[] args) throws IOException {
//获得路径path
String path = JsoupTest.class.getClassLoader().getResource("student.xml").getPath();
//解析
Document document = Jsoup.parse(new File(path), "utf-8");
//查询name标签
Elements elements = document.select("name");
System.out.println(elements.get(0).text());
//查询id
Elements id = document.select("#cat");
System.out.println(elements.get(0).select("name").text());
System.out.println("******************");
//查找student中number等于heima_0001
Elements select = document.select("student[number=\"heima_0001\"]");
System.out.println(select);
System.out.println("******************");
//查找student中number等于heima_0001中的age子标签
Elements select1 = document.select("student[number=\"heima_0001\"]>age");
System.out.println(select1);
}
}
XPath:
解释:
XPath 是一门在 XML 文档中查找信息的语言。
XPath 是 XSLT 中的主要元素。
XQuery 和 XPointer 均构建于 XPath 表达式之上
使用Jsoup的xpath需要额外导入jar包
查询w3cschool参考手册,使用xpath语法完成
public class JsoupXpath {
public static void main(String[] args) throws IOException, XpathSyntaxErrorException {
//获得路径path
String path = JsoupTest.class.getClassLoader().getResource("student.xml").getPath();
//解析
Document document = Jsoup.parse(new File(path), "utf-8");
//剧创建JXDocumnet对象
JXDocument jxDocument=new JXDocument(document);
//结合xpath语法查询
List<JXNode> jxNodes = jxDocument.selN("//student");
System.out.println(jxNodes);
System.out.println("__________________________");
List<JXNode> jxNode = jxDocument.selN("//student[@number='heima_0001']");
System.out.println(jxNode);
}
}
原文:https://www.cnblogs.com/PoetryAndYou/p/11295811.html