I want to find the element "buddyname" and get the element of value= "" in a HTML file which i put into a StringBuffer, in this case 5342test. The element in value= "" can change so i can not search directly for 5342test.
<fieldset style="display:none"><input type="hidden" name="buddyname" value="5342test"/></fieldset>
How can i do this with jsoup? or is there an easier way, I already tried Pattern/Matcher but that did not work out as i had issues with the Pattern.compile("<input[^>]*?value\\s*?=\\s*?\\\"(.*?)\\\")");
Below some example code. Thank you in advance.
Document doc = Jsoup.parse(page); // page is a StringBuffer
Elements td = doc.select("fieldset");
for (Element td : tds) {
String tdText = td.text();
System.out.println(tdText);
}
With XPath expressions it is able to select the elements within the HTML using Jsoup as HTML parser.
A HTML element consists of a tag name, attributes, and child nodes (including text nodes and other elements). From an Element, you can extract data, traverse the node graph, and manipulate the HTML.
Jsoup can also be used to parse and build XML.
clean. Creates a new, clean document, from the original dirty document, containing only elements allowed by the safelist. The original document is not modified. Only elements from the dirty document's body are used.
Just use the attribute selector [attrname=attrvalue]
.
Element buddynameInput = document.select("input[name=buddyname]").first();
String buddyname = buddynameInput.attr("value");
// ...
Do not use regex to parse HTML. It makes no sense if you already have a world class HTML parser at your hands.
Selector
API documentationIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With