一、步骤
- 导入 jar 包
- 获取 Document 对象
- 获取对应的标签(Element)
- 获取标签数据
二、用到的类
- Jsoup:工具类,可以解析html或xml文档,返回 Document
- Document:文档对象。代表内存中的dom树
- Elements:元秦E1ement对象的集合。可以当做 ArrayList< Element>来使用
- Node:节点对象
三、实战
1. student.xsd 约束文件
<?xml version="1.0"?>
<xsd:schema xmlns="http://www.lanyue.cn/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.lanyue.cn/xml" elementFormDefault="qualified">
<xsd:element name="students" type="studentsType"/>
<xsd:complexType name="studentsType">
<xsd:sequence>
<xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="studentType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="ageType" />
<xsd:element name="sex" type="sexType" />
</xsd:sequence>
<xsd:attribute name="number" type="numberType" use="required"/>
</xsd:complexType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="256"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
2. student.xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.lanyue.cn/xml"
xsi:schemaLocation="http://www.lanyue.cn/xml student.xsd"
>
<student number="0001">
<name>小红</name>
<age>18</age>
<sex>male</sex>
</student>
<student number="0002">
<name>小明</name>
<age>18</age>
<sex>female</sex>
</student>
</students>
3.jar离线包
https://download.csdn.net/download/ITlanyue/12710441
4.测试入口类
(1)入门
package cn.lanyue.xml.jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
public class JsoupDemo1 {
public static void main(String[] args) throws IOException {
//2.获取Document对象,根据xml文档获取
//2.1获取student.xml的path
String path = "G:\火狐下载\day32_xml\src\resources\student.xml";
//2.2解析xml文档,加载文档进内存,获取dom树--->Document
// 字符集必须和文件编码格式一致
Document document = Jsoup.parse(new File(path), "utf-8");
//3.获取元素对象 Element 的 name 标签
Elements elements = document.getElementsByTag("name");
for(Element e: elements){
System.out.println(e.text());
}
}
}
(2)Jsoup parse 构造函数
package cn.lanyue.xml.jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* parse 的构造方法
*/
public class JsoupDemo2 {
public static void main(String[] args) throws IOException {
// 2.1获取student.xml的path
String path = "G:\火狐下载\day32_xml\src\resources\student.xml";
// 2.2解析xml文档,加载文档进内存,获取dom树--->Document
Document document = Jsoup.parse(new File(path), "utf-8");
System.out.println(document);
// 2.parse(String html):参数为html或xml文档的内容
//
// 3.parse(URL url, int timeoutMillis):通过网络路径获取指定的html或xml的文档对象
URL url = new URL("https://www.baidu.com");
// 参数为超时时间
Document documentUrl = Jsoup.parse(url, 10000);
System.out.println(documentUrl);
}
}
(3)Document /Element 对象的使用
package cn.lanyue.xml.jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
/**
* Document/Element对象功能
*/
public class JsoupDemo3 {
public static void main(String[] args) throws IOException {
//1.获取student.xml的path
String path = "G:\火狐下载\day32_xml\src\resources\student.xml";
//2.获取Document对象
Document document = Jsoup.parse(new File(path), "utf-8");
//3.获取元素对象了。
//3.1获取所有student对象
Elements elements = document.getElementsByTag("student");
System.out.println(elements);
System.out.println("-----------");
//3.2 获取属性名为id的元素对象们
Elements elements1 = document.getElementsByAttribute("number");
System.out.println(elements1);
System.out.println("-----------");
//3.2获取 number属性值为heima_0001的元素对象
Elements elements2 = document.getElementsByAttributeValue("number", "0001");
System.out.println(elements2);
}
}
(4)Document详细属性
package cn.lanyue.xml.jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
/**
* 获取Document对象的各种属性值
*/
public class JsoupDemo4 {
public static void main(String[] args) throws IOException {
//1.获取student.xml的path
String path = "G:\火狐下载\day32_xml\src\resources\student.xml";
//2.获取Document对象
Document document = Jsoup.parse(new File(path), "utf-8");
/*
Element:元素对象
1. 获取子元素对象
* getElementById(String id):根据id属性值获取唯一的element对象
* getElementsByTag(String tagName):根据标签名称获取元素对象集合
* getElementsByAttribute(String key):根据属性名称获取元素对象集合
* getElementsByAttributeValue(String key, String value):根据对应的属性名和属性值获取元素对象集合
2. 获取属性值
* String attr(String key):根据属性名称获取属性值
3. 获取文本内容
* String text():获取所有字标签的纯文本内容
* String html():获取标签体的所有内容(包括子标签的标签和文本内容)
*/
//通过Document对象获取name标签,获取所有的name标签,可以获取到两个
Elements elements = document.getElementsByTag("name");
System.out.println(elements.size());
System.out.println("-----------");
//通过Element对象获取子标签对象
Element element_student = document.getElementsByTag("student").get(0);
Elements ele_name = element_student.getElementsByTag("name");
System.out.println(ele_name.size());
//获取student对象的属性值
String number = element_student.attr("NUMBER");
System.out.println(number);
System.out.println("-----------");
//获取文本内容
String text = ele_name.text();
String html = ele_name.html();
System.out.println(text);
System.out.println(html);
}
}
5.选择器查询
package cn.lanyue.xml.jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
/**
* 选择器查询
*/
public class JsoupDemo5 {
public static void main(String[] args) throws IOException {
//1.获取student.xml的path
String path = "G:\火狐下载\day32_xml\src\resources\student.xml";
//2.获取Document对象
Document document = Jsoup.parse(new File(path), "utf-8");
//3.查询name标签
Elements elements = document.select("name");
System.out.println(elements);
System.out.println("=----------------");
//4.查询id值为的元素
Elements elements1 = document.select("#0001");
System.out.println(elements1);
System.out.println("----------------");
//5.获取student标签并且number属性值为0001的age子标签
//5.1.获取student标签并且number属性值为0001
Elements elements2 = document.select("student[number="0001"]");
System.out.println(elements2);
System.out.println("----------------");
//5.2获取student标签并且number属性值为0001的age子标签
Elements elements3 = document.select("student[number="0001"] > age");
System.out.println(elements3);
}
}
6.Xpath查询
package cn.lanyue.xml.jsoup;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
*XPath查询
*/
public class JsoupDemo6 {
public static void main(String[] args) throws IOException, XpathSyntaxErrorException {
//1.获取student.xml的path
String path = "G:\火狐下载\day32_xml\src\resources\student.xml";
//2.获取Document对象
Document document = Jsoup.parse(new File(path), "utf-8");
//3.根据document对象,创建JXDocument对象
JXDocument jxDocument = new JXDocument(document);
//4.结合xpath语法查询
//4.1查询所有student标签
List<JXNode> jxNodes = jxDocument.selN("//student");
for (JXNode jxNode : jxNodes) {
System.out.println(jxNode);
}
System.out.println("--------------------");
//4.2查询所有student标签下的name标签
List<JXNode> jxNodes2 = jxDocument.selN("//student/name");
for (JXNode jxNode : jxNodes2) {
System.out.println(jxNode);
}
System.out.println("--------------------");
//4.3查询student标签下带有id属性的name标签
List<JXNode> jxNodes3 = jxDocument.selN("//student/name[@id]");
for (JXNode jxNode : jxNodes3) {
System.out.println(jxNode);
}
System.out.println("--------------------");
//4.4查询student标签下带有id属性的name标签 并且id属性值为itcast
List<JXNode> jxNodes4 = jxDocument.selN("//student/name[@id='0001']");
for (JXNode jxNode : jxNodes4) {
System.out.println(jxNode);
}
}
}