3

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

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

As i found out from other posts, i should be using one of the HTML parsers (not regex). Could you please tell me which one to use or show a code sample.

Thank you.

jmj
  • 237,923
  • 42
  • 401
  • 438
DixieFlatline
  • 7,895
  • 24
  • 95
  • 147

2 Answers2

4

You could 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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

Have a look at http://htmlparser.sourceforge.net/

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92