Jsoup和JsoupXpath来解析html和xml文件

Jsoup介绍

jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。同时也可以使用jsoup来解析xml文件
主要功能

  1. 从一个URL,文件或字符串中解析HTML

  2. 使用DOM或CSS选择器来查找、取出数据使用DOM或CSS选择器来查找、取出数据

  3. 可操作HTML元素、属性、文本可操作HTML元素、属性、文本

1、用Jsoup解析xml

<?xml version="1.0"?>
<students>
    <student id="1" sex="">
        <name>小民</name>
        <age>93</age>
    </student>

    <student id="2" sex="">
        <name>小泽</name>
        <age>13</age>
    </student>
</students>
public static void main(String args[]) throws IOException {
        Document parse = Jsoup.parse(new File("src/Jsoup/student.xml"),"utf-8");
        //获取student标签
        Elements students = parse.getElementsByTag("student");
        for (Element student:students){
        //获取标签的属性集合
            Attributes attributes = student.attributes();
            for (Attribute attribute:attributes){
                System.out.println(attribute.getKey()+"="+attribute.getValue());
            }
            //获取所有标签child
            Elements children = student.children();
            for (Element c:children){
                System.out.println(c.tag().getName()+":"+c.text());
            }
        }
    }

结果
在这里插入图片描述
2、Jsoup解析HTML

public static void main(String args[]) throws IOException {
        System.out.println("方法1使用parse传入一个URL对象");
        URL url = new URL("http://www.baidu.com");
        Document parse = Jsoup.parse(url, 3000);
        //获取结果的title
        System.out.println(parse.title());
        System.out.println("方法2使用connect()直接连接");
        Connection connect = Jsoup.connect("http://www.baidu.com");
        System.out.println(connect.get().title());
    }

结果
在这里插入图片描述
3、Jsoup的css选择器

public static void main(String args[]) throws IOException {
        Document document = Jsoup.connect("http://www.baidu.com").get();
        //选取所有含有href属性的结点
        System.out.println(document.select("[href]"));
        System.out.println("----------------------------------------------------------");
        //选取所有含有href属性里含有baidu的结点
        System.out.println(document.select("[href~=baidu]"));
    }

部分结果
在这里插入图片描述

Jsoup的Xpath查询

xpath

XPath 是一门在 XML 文档中查找信息的语言。

参考xpath教程:https://www.runoob.com/xpath/xpath-tutorial.html

需要这4个jar包
在这里插入图片描述

路径表达式结果
bookstore选取 bookstore 元素的所有子节点。
/bookstore选取根元素 bookstore。注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径!
bookstore/book选取属于 bookstore 的子元素的所有 book 元素。
//book选取所有 book 子元素,而不管它们在文档中的位置。
bookstore//book选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。
//@lang选取名为 lang 的所有属性。
/bookstore/book[1]选取属于 bookstore 子元素的第一个 book 元素。
/bookstore/book[last()]选取属于 bookstore 子元素的最后一个 book 元素。
/bookstore/book[last()-1]选取属于 bookstore 子元素的倒数第二个 book 元素。
/bookstore/book[position()❤️]选取最前面的两个属于 bookstore 元素的子元素的 book 元素。
//title[@lang]选取所有拥有名为 lang 的属性的 title 元素。
//title[@lang=‘eng’]选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。
/bookstore/book[price>35.00]选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须大于 35.00。
/bookstore/book[price>35.00]//title选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须大于 35.00。

使用测试

public static void main(String args[]) throws IOException {
        Document document = Jsoup.parse(new File("src/Jsoup/student.xml"), "utf-8");
        JXDocument jxDocument = JXDocument.create(document);
        //1.
        List<JXNode> jxNodes = jxDocument.selN("//student");
        System.out.println(jxNodes);
        System.out.println("++++++++++++++++++++++++++++++");
        //2.
        JXNode jxNode = jxDocument.selNOne("//student[@id='1']/name");
        System.out.println(jxNode.asElement().text());
    }

结果
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值