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

ArrayListconvertLinksToStrings(Elements links)
convert Links To Strings
ArrayList<String> result = new ArrayList<String>();
for (Element link : links) {
    result.add(link.attr("abs:href"));
return result;
StringconvertNodeToText(Element element)
convert Node To Text
if (element == null)
    return "";
final StringBuilder buffer = new StringBuilder();
new NodeTraversor(new NodeVisitor() {
    @Override
    public void head(Node node, int depth) {
        if (node instanceof TextNode) {
            TextNode textNode = (TextNode) node;
...
voidconvertTablesToDivs(Element body)
convert Tables To Divs
Elements elements = body.select("table");
for (Element element : elements) {
    element.tagName("div");
elements = body.select("tbody");
for (Element element : elements) {
    element.tagName("div");
elements = body.select("tr");
for (Element element : elements) {
    element.tagName("div");
elements = body.select("th");
for (Element element : elements) {
    element.tagName("p");
elements = body.select("td");
for (Element element : elements) {
    element.tagName("p");
Elementcopy(Element e)
copy
Element res = new Element(Tag.valueOf(e.tagName()), e.baseUri());
for (Attribute a : e.attributes()) {
    res.attr(a.getKey(), a.getValue());
res.html(e.html());
return res;
intcountElements(Node parent)
count Elements
int count = 0;
List<Node> childNodes = parent.childNodes();
for (Node childNode : childNodes) {
    if (childNode instanceof Element) {
        count++;
        count += countElements(childNode);
return count;
ElementcreateSafeElement(Element sourceEl)
create Safe Element
String sourceTag = sourceEl.tagName();
Attributes destAttrs = new Attributes();
Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs);
Attributes sourceAttrs = sourceEl.attributes();
for (Attribute sourceAttr : sourceAttrs) {
    destAttrs.put(sourceAttr);
return dest;
...
ListelementsToList(Elements elements)
elements To List
List<String> elementsList = new ArrayList<String>();
for (Element element : elements) {
    elementsList.add(element.toString());
return elementsList;
StringextractTextWithNewlines(Element elem)
Extracts text from Element, replacing
with newline.
elem.select("br").html(LINE_SEPARATOR);
return elem.text().replace(LINE_SEPARATOR, "\n");
voidfilterElementsByTag(List results, Element element, Set tagSet)
filter Elements By Tag
if (tagSet.contains(element.tag().getName().toLowerCase()))
    results.add(element);
for (Element child : element.children())
    filterElementsByTag(results, child, tagSet);
ElementsfindAElementsWithId(Elements elements, String id)
find A Elements With Id
StringBuilder query = new StringBuilder();
query.append("a[id=");
query.append(id);
query.append("]");
Elements found = elements.select(query.toString());
return found;