How to get value from HTML file in Java?

I need to get the value ("abc" in the example below) from an HTML file that looks like this:

          <input type="hidden" name="something" value="abc" />

As I learned from other posts, I have to use one of the HTML parsers (not a regular expression). Could you tell me which one to use or show a sample code.

Thank.

+3
source share
2 answers

You can use Jsoup for this.

File file = new File("/path/to/file.html");
Document document = Jsoup.parse(file, "UTF-8");
Element something = document.select("input[name=something]").first();
String value = something.val();
System.out.println(value); // abc
// ...

Or shorter:

String value = Jsoup.parse(new File("/path/to/file.html"), "UTF-8").select("input[name=something]").first().val();
System.out.println(value); // abc
// ...

See also:

+4
source

Source: https://habr.com/ru/post/1783428/


All Articles