1. 程式人生 > 其它 >xml_解析_Jsoup_根據選擇器查詢與xml_解析_Jsoup_根據Xpath查詢

xml_解析_Jsoup_根據選擇器查詢與xml_解析_Jsoup_根據Xpath查詢

xml_解析_Jsoup_根據選擇器查詢

   快捷查詢方式:

      1. selector:選擇器
         使用的方法:Elements select​(String cssQuery)
           語法:參考Selector類中定義的語法

package ch.xueqiang.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 JsoupDemo5 { public static void main(String[] args) throws IOException { //1.獲取student.xml的path String path = JsoupDemo5.class.getClassLoader().getResource("student.xml").getPath(); //2.獲取Document物件 Document document = Jsoup.parse(new
File(path), "utf-8"); //3.查詢name標籤 /* div{ } */ Elements elements = document.select("name"); System.out.println(elements); System.out.println("=----------------"); //4.查詢id值為itcast的元素 Elements elements1 = document.select("#itcast"); System.out.println(elements1); System.out.println(
"----------------"); //5.獲取student標籤並且number屬性值為heima_0001的age子標籤 //5.1.獲取student標籤並且number屬性值為heima_0001 Elements elements2 = document.select("student[number=\"heima_0001\"]"); System.out.println(elements2); System.out.println("----------------"); //5.2獲取student標籤並且number屬性值為heima_0001的age子標籤 Elements elements3 = document.select("student[number=\"heima_0001\"] > age"); System.out.println(elements3); } }

 

xml_解析_Jsoup_根據Xpath查詢

2. XPath:XPath即為XML路徑語言,它是一種用來確定XML(標準通用標記語言的子集)文件中某部分位置的語言
      使用Jsoup的Xpath需要額外匯入jar包。
           查詢w3cshool參考手冊,使用xpath的語法完成查詢
            程式碼:

    

//1.獲取student.xml的path
                    String path = JsoupDemo6.class.getClassLoader().getResource("student.xml").getPath();
                    //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='itcast']");
                    for (JXNode jxNode : jxNodes4) {
                        System.out.println(jxNode);
                    }