Java Utililty Methods HTML Jsoup Element

List of utility methods to do HTML Jsoup Element

Description

The list of methods to do HTML Jsoup Element are organized into topic(s).

Method

OptionalgetFirstWithOwnText(final Elements elementList, final String text)
get First With Own Text
return elementList.stream().filter(el -> el.ownText().equals(text)).findFirst();
StringgetFormattedText(Element element)
Given a jsoup element, gets all contained text preserving formatting by tags such as
StringBuilder stringBuilder = new StringBuilder();
element.childNodes().forEach(node -> {
    if (node instanceof TextNode) {
        stringBuilder.append(((TextNode) node).text());
    } else if (node instanceof Element) {
        if ("br".equalsIgnoreCase(((Element) node).tag().getName())) {
            stringBuilder.append("\n");
        } else {
...
StringgetIndustry(Element element)
get Industry
return element.select("span.industry").text();
intgetInt(Elements td, int i)
get Int
String s = getString(td, i);
String n = isNullOrEmpty(s) ? "-1" : s;
return Integer.parseInt(n);
StringgetLink(Element element, int index)
get Link
try {
    List<Element> elements = element.getElementsByTag("a");
    return elements.get(index).attr("href");
} catch (IndexOutOfBoundsException e) {
    throw new NullPointerException();
intgetMoney(final Element container)
get Money
final List<Element> allMoneyElements = new ArrayList<Element>();
allMoneyElements.addAll(container.select("span.moneygold"));
allMoneyElements.addAll(container.select("span.moneysilver"));
allMoneyElements.addAll(container.select("span.moneycopper"));
if (allMoneyElements.isEmpty()) {
    return 0;
int money = 0;
...
StringgetNameFromAttributes(org.jsoup.nodes.Element e)
checks whether the attributes values to be used as variable name
String id = e.attr("id");
String name = e.attr("name");
String value = e.attr("value");
String _class = e.attr("class");
String action = e.attr("action");
String text = e.text();
text = text.replaceAll("\t", "_");
text = text.replaceAll("\n", "_");
...
ElementgetOneSubItemOfClass(Element parent, String className)
get One Sub Item Of Class
return getOneSubItemOfClass(parent, "div", className);
StringgetReviewAuthorIDFromMultipleSelect(Element doc, String location, String locationValue)
get Review Author ID From Multiple Select
String author_id = getAttributeFromMultipleSelect(doc, location, locationValue);
if (!author_id.equals("none")) {
    author_id = (author_id != null) ? (author_id.split("=")[1]) : "none";
return author_id.trim();
ElementgetSingleElement(final Elements elements)
get Single Element
if (elements.size() != 1) {
    throw new IllegalArgumentException(
            "A single element matching the criteria was expected.  Instead, found " + elements.size());
return elements.first();