Java HTML Jsoup Element findElementsByTag(Element element, String... tags)

Here you can find the source of findElementsByTag(Element element, String... tags)

Description

Returns the all elements matching any of the given tags (case-insensitive).

License

Mozilla Public License

Declaration

public static Elements findElementsByTag(Element element, String... tags) 

Method Source Code

//package com.java2s;
/**/*  w  w  w  . jav  a  2 s .co m*/
 * Copyright (C) 2012-2014 Gist Labs, LLC. (http://gistlabs.com)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main {
    /** Returns the all elements matching any of the given tags (case-insensitive). */
    public static Elements findElementsByTag(Element element, String... tags) {
        List<Element> results = new ArrayList<Element>();

        Set<String> tagSet = new HashSet<String>();
        for (String tag : tags)
            tagSet.add(tag.toLowerCase());
        filterElementsByTag(results, element, tagSet);
        return new Elements(results);
    }

    private static void filterElementsByTag(List<Element> results, Element element, Set<String> tagSet) {
        if (tagSet.contains(element.tag().getName().toLowerCase()))
            results.add(element);

        for (Element child : element.children())
            filterElementsByTag(results, child, tagSet);
    }
}

Related

  1. elementsToList(Elements elements)
  2. extractTextWithNewlines(Element elem)
  3. filterElementsByTag(List results, Element element, Set tagSet)
  4. findAElementsWithId(Elements elements, String id)
  5. findAllNodesBefore(Element firstChapter)
  6. findFirstByTag(Element element, String tag)
  7. findNextElementSibling(final Element startElement, final Predicate condition)
  8. findPreviousH2Element(Element h3)
  9. findPreviousHElement(Elements sequence, Element reference)