How to get a value from HTML file in Java?
I need to get a value ("abc" in below example) from HTML file that looks like this:
<input type="hidden" name="some开发者_开发百科thing" 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.
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:
- What are the pros and cons of leading Java HTML parsers?
Have a look at http://htmlparser.sourceforge.net/
精彩评论