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);
    }
}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM