使用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方法导航文档