Java HTML Parse Jsoup coverTag(String html, String... tagNames)

Here you can find the source of coverTag(String html, String... tagNames)

Description

cover html by tagNames by first element of tagName will be outermost.

License

Open Source License

Parameter

Parameter Description
html input html
tagNames multiple tags name

Return

element that covered by those tags

Declaration

public static Element coverTag(String html, String... tagNames) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.*;

public class Main {
    private static Document.OutputSettings setting;

    /**/*from   w ww.  j  a  v a2  s. co  m*/
     * cover {@code html} by {@code tagNames} by first element of tagName will be outermost. <br>
     * <pre>{@code
     *  input (html): hello world
     *  input (tags): [html, body, div]
     *
     *  output:
     *  <html>
     *      <body>
     *          <div>hello world</div>
     *      </body>
     *  </html>
     * }</pre>
     *
     * @param html
     *       input html
     * @param tagNames
     *       multiple tags name
     * @return element that covered by those tags
     */
    public static Element coverTag(String html, String... tagNames) {
        List<String> tags = Arrays.asList(tagNames);
        Collections.reverse(tags);
        Element e = null;
        for (String s : tags) {
            if (e == null)
                e = addTag(html, s);
            else
                e = addTag(e, s);
        }
        return e;
    }

    private static Element addTag(String fullHtml, String tagName) {
        return parse(fullHtml).body().tagName(tagName);
    }

    private static Element addTag(Element e, String tagName) {
        return parse(e.toString()).body().tagName(tagName);
    }

    /**
     * convert html String to {@link Document} (A lot more easier to manage it)
     *
     * @param html
     *       input html
     * @return Document (include html body and head Tag)
     * @see Document
     * @see Document#head()
     * @see Document#body()
     */
    public static Document parse(String html) {
        Document document = Jsoup.parse(html);
        if (setting != null)
            return document.outputSettings(setting);
        return document;
    }
}

Related

  1. cleanHtmlCode(String html)
  2. cleanHtmlFromString(String stringToClean)
  3. cleanHTMLTags(String str)
  4. cleanupHtmlDoc(String s)
  5. clearBody(String html)
  6. extractRssUrl(String html, URI base)
  7. filter(String html)
  8. fixHtml(String htmlContent, String outputFile, String contentFile)
  9. getContentFromHTML(String html)