Parsing input element using JSoup

狂风中的少年 提交于 2019-12-22 10:53:42

问题


JSoup is used to parse the following html

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

Here is the code of JSoup

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

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


回答1:


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)

You got several options:

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

or

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

or

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

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

Use selector-syntax to find elements

Use DOM methods to navigate a document



来源:https://stackoverflow.com/questions/11068546/parsing-input-element-using-jsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!