使用JSoup解析输入元素 [英] Parsing input element using JSoup

查看:71
本文介绍了使用JSoup解析输入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSoup用于解析以下html

JSoup is used to parse the following html

<input type="checkbox" id="id12" name="renewalCheckboxGroup" value="check1" class="wicket-id11" /> 

这是JSoup的代码

    Document document = Jsoup.parse("<input type=\"checkbox\" id=\"id12\" name=\"renewalCheckboxGroup\" value=\"check1\" class=\"wicket-id11\" />");
    System.out.println(document.id());

预期结果应为id12,但是返回的id为空字符串. 我也尝试调用attribute("id")函数,但仍然徒劳. 怎么解决呢?谢谢YOu

Expected result should be id12, however, the returned id is an empty string. I also try to call attribute("id") function as well, but still in vain. How to solve it? Thank YOu

推荐答案

据我所知,您应该从document中选择/查找/提取所需的Element,然后才访问其属性(id例子)

As far as I know you should select/find/extract your desired Element from your document and only then access its attribute (id for example)

您有几种选择:

Elements inputs = document.getElementsByTag("input"); //then access the one at 0 index

Element input = doc.getElementById("id12");

Elements inputs = doc.select("input[name=renewalCheckboxGroup]"); //then access the one at 0 index

查看文档以了解更多选项...

take a look at the docs for more options...

使用选择器语法查找元素

使用DOM方法导航文档

这篇关于使用JSoup解析输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆