File size: 1,474 Bytes
08aee07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cn.itcast.xml.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 * 快捷查询方式:1、选择器selector
 */
public class JsoupDemo5 {
    public static void main(String[] args) throws IOException {
        /*
			1. selector:选择器
				* 使用的方法:Elements	select(String cssQuery)
					* 语法:参考Selector类中定义的语法
         */
        //1、获取document对象
        String path = JsoupDemo5.class.getClassLoader().getResource("student1.xml").getPath();
        Document document = Jsoup.parse(new File(path), "utf-8");

        //2、查询name标签
        Elements names = document.select("name");
        System.out.println(names);
        System.out.println("====================");

        //3、查询id值为1的元素
        Elements id1 = document.select("#1");
        System.out.println(id1);
        System.out.println("====================");

        //4、获取student标签,并且其number为s001,获取其age子标签
        Elements student2 = document.select("student[number='s001'] age");//所有age子标签
        System.out.println(student2);
        System.out.println("====================");

        Elements student3 = document.select("student[number='s001'] > age");//一级age子标签
        System.out.println(student3);
        System.out.println("====================");
    }
}