使用JSoup解析输入元素

Bea*_*ear 3 html-parsing jsoup

JSoup用于解析以下html

<input type="checkbox" id="id12" name="renewalCheckboxGroup" value="check1" class="wicket-id11" /> 
Run Code Online (Sandbox Code Playgroud)

这是JSoup的代码

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

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

Dan*_*iel 7

据我所知,你应该从你的中选择/查找/提取你想要的Element,document然后才能访问它的属性(id例如)

你有几个选择:

Elements inputs = document.getElementsByTag("input"); //then access the one at 0 index
Run Code Online (Sandbox Code Playgroud)

要么

Element input = doc.getElementById("id12");
Run Code Online (Sandbox Code Playgroud)

要么

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

看看文档以获取更多选项......

使用selector-syntax查找元素

使用DOM方法导航文档