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

 

posted @ 2022-12-02 17:29  zj勇敢飞,xx永相随  阅读(38)  评论(0编辑  收藏  举报