Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jsoup get element in value=" "

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);
        } 
like image 496
Lars Avatar asked Sep 25 '11 14:09

Lars


People also ask

Can we use XPath in jsoup?

With XPath expressions it is able to select the elements within the HTML using Jsoup as HTML parser.

What is element in jsoup?

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.

Can jsoup parse XML?

Jsoup can also be used to parse and build XML.

What does jsoup clean do?

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.


1 Answers

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.

See also:

  • Jsoup CSS selector syntax cookbook
  • Jsoup Selector API documentation
like image 181
BalusC Avatar answered Sep 28 '22 05:09

BalusC