java解析html

html 在浏览器使用js可以很简单的解析出来,获取自己所需要的内容或者值。
在Java代码里我们可以使用:

javax.xml.parsers.DocumentBuilder

来解析html,来获取需要的内容或者值

来个示例:

    public void test6() throws Exception {
    	//获取DocumentBuilder 
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        //需要解析html
        String html = "<span id='spanName_Q85fcFzaEY' attendee-id='5490'>tester1</span><div id='num_Q85fcFzaEY' class='attendee'>10</div>";
        //这里需要一个完整的开始标签和结束标签,可以使用其他的,如果你的html是完整的,可以不加。
        String html = "<a>"+html+"</a>";
        Document document = documentBuilder.parse(new ByteArrayInputStream( html.getBytes()));
        System.out.println(document.getElementsByTagName("span").item(0).getAttributes().getNamedItem("attendee-id").getNodeValue());
        System.out.println(document.getElementsByTagName("div").item(0).getTextContent());
    }

输出结果:

5490
10
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值