Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing input element using JSoup

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

like image 613
Bear Avatar asked Dec 06 '25 13:12

Bear


1 Answers

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

like image 135
Daniel Avatar answered Dec 10 '25 03:12

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!