Java HTML Parse Jsoup getHtmlInTag(String html, String tag)

Here you can find the source of getHtmlInTag(String html, String tag)

Description

get html tag include tag too
 input (html): 
Hello world!
input (tag): code output: Hello world
so you can remove the input tag by using #removeTag(String)

License

Open Source License

Parameter

Parameter Description
html searching html
tag tagName (learn more Tag#valueOf(String) )

Return

(which contains of html string inside tag parameter)

Declaration

public static Elements getHtmlInTag(String html, String tag) 

Method Source Code


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

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

import org.jsoup.select.Elements;

public class Main {
    private static Document.OutputSettings setting;

    /**//from  w w  w. j a v a2s.  com
     * get html tag include tag too <br>
     * <pre>{@code
     * input (html): <div><code>Hello world</code>!</div>
     * input (tag): code
     *
     * output: <code>Hello world</code>
     *     }</pre>
     * so you can remove the input tag by using {@link #removeTag(String)}
     *
     * @param html
     *       searching html
     * @param tag
     *       tagName (learn more {@link Tag#valueOf(String)})
     * @return {@link Elements} (which contains of html string inside tag parameter)
     * @see HtmlUtil#removeTag(String)
     */
    public static Elements getHtmlInTag(String html, String tag) {
        return parse(html).child(0).getElementsByTag(tag);
    }

    /**
     * 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. getFirstImageSrc(String html)
  2. getFirstSentence(final String html)
  3. getFirstTableFromHTML(String result)
  4. getHtml(String url, String ruta_fich)
  5. getHtmlBodyContent(String html)
  6. getImageCredit(String html)
  7. getJSFileLinks(String html)
  8. getMetaValue(String html, String metaKey)
  9. getPlainText(String htmlText)