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

voidaddAllNodes(Element parent, List childNodes)
add All Nodes
for (Node child : childNodes) {
    parent.appendChild(child);
voidaddProperty(Map ret, Element element)
add Property
throw new UnsupportedOperationException("Fix This"); 
voidaddStyle(Element element, String styleToBeAdded)
Add style to existing one.
String style = element.attr(ATTRIBUTE_STYLE);
style += styleToBeAdded;
element.attr(ATTRIBUTE_STYLE, style);
ElementaddTag(Element e, String tagName)
add Tag
return parse(e.toString()).body().tagName(tagName);
voidappendFragment(Element e, String fragment)
append Fragment
Element doc = Jsoup.parse(fragment);
appendFragment(e, doc);
voidappendText(Element element, StringBuffer stringBuffer)
append Text
List<Node> nodes = element.childNodes();
if (nodes != null && nodes.size() > 0) {
    for (int i = 0; i < nodes.size(); i++) {
        Node node = nodes.get(i);
        if (node instanceof TextNode) {
            String text = ((TextNode) node).text();
            if (text.trim().length() > 0) {
                stringBuffer.append(text);
...
IterablechildrenByTag(final Element element, final String tag)
children By Tag
return Iterables.filter(element.children(), new Predicate<Element>() {
    public final boolean apply(final Element _element) {
        return _element.tagName().equals(tag);
});
voidcollectItemRewards(final Element icontab, final BiConsumer collector)
collect Item Rewards
Objects.requireNonNull(icontab);
final Map<String, String> itemNamesByIconId = new LinkedHashMap<>();
final Map<String, Integer> itemQuantitiesByIconId = new LinkedHashMap<>();
for (final Element iconPlaceholder : icontab.select("th[id]")) {
    final String iconId = iconPlaceholder.id();
    final String itemName = iconPlaceholder.nextElementSibling().getElementsByTag("a").first().ownText();
    itemNamesByIconId.put(iconId, itemName);
final List<Element> itemScripts = icontab.ownerDocument().select("script:containsData(icontab)");
for (final Element script : itemScripts) {
    final Pattern iconInitRegex = Pattern.compile(
            Pattern.quote("$WH.ge('") + "([^']+)" + Pattern.quote("').appendChild(") + "[A-Za-z0-9_]+"
                    + Pattern.quote(".createIcon(") + "[^\"]+\"([^\"]+)\"" + Pattern.quote("));"));
    final Matcher matcher = iconInitRegex.matcher(script.data());
    while (matcher.find()) {
        itemQuantitiesByIconId.put(matcher.group(1), Integer.parseInt(matcher.group(2)));
itemNamesByIconId.forEach((iconId, itemName) -> {
    Integer itemQuantity = itemQuantitiesByIconId.get(iconId);
    if (itemQuantity != null && (itemQuantity == 0 || itemQuantity == 1)) {
        itemQuantity = null;
    collector.accept(itemName, itemQuantity);
});
StringcollectTextUntilNextTag(final Element header, final String nextTagName)
collect Text Until Next Tag
final StringBuilder sb = new StringBuilder();
for (Node node = header.nextSibling(); !(node instanceof Element
        && ((Element) node).tagName().equals(nextTagName)); node = node.nextSibling()) {
    if (node instanceof TextNode) {
        sb.append(((TextNode) node).text());
    } else if (node instanceof Element && ((Element) node).tagName().equals("br")) {
        sb.append("\n");
return sb.toString();
booleancontainsLink(Element element, String link)
contains Link
int links = 0;
for (Element child : element.children()) {
    if (child.tagName().equals("a") && child.hasAttr("href") && child.attr("href").contains(link))
        links++;
return links > 0;