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 JsoupDemo4 {
    public static void main(String[] args) throws IOException {
        String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath();
        Document document = Jsoup.parse(new File(path),"utf-8");
        //获取子元素
        Element student = document.getElementsByTag("student").get(1);
        System.out.println(student.getElementsByTag("name"));
        System.out.println("----------------");
        //获取属性值
        String attr = student.attr("number");
        System.out.println(attr);
        System.out.println("----------------");
        //获取文本内容
        String text = student.text();
        //text获取纯文本内容
        System.out.println(text);
        String html = student.html();
        //html获取文本标签和文本内容
        System.out.println(html);
    }
}


posted @ 2021-05-05 22:18  code-G  阅读(1706)  评论(0编辑  收藏  举报