Java HTML Parse Jsoup removeTag(String html)

Here you can find the source of removeTag(String html)

Description

remove top tag and return as string
 input: 
I am Java programmer
output: I am Java programmer
The output, can managing by #newOutputSetting(Document.OutputSettings)

License

Open Source License

Parameter

Parameter Description
html input html

Return

string that removed top tag

Declaration

public static String removeTag(String html) 

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 va 2 s.com
     * remove top tag and return as string <br>
     * <pre>{@code
     *  input: <div>I am <code>Java</code> programmer</div>
     *  output: I am <code>Java</code> programmer
     * }</pre>
     * <b>The output, can managing by {@link #newOutputSetting(Document.OutputSettings)}</b>
     *
     * @param html
     *       input html
     * @return string that removed top tag
     */
    public static String removeTag(String html) {
        return parse(html).child(0).html();
    }

    /**
     * remove top tag and return as string <br>
     * <b>The output, can managing by {@link #newOutputSetting(Document.OutputSettings)}</b>
     *
     * @param html
     *       input Elements (easy get from {@link #getHtmlInTag(String, String)})
     * @return string that removed top tag
     * @see #removeTag(String)
     */
    public static String removeTag(Elements html) {
        return parse(html.toString()).child(0).html();
    }

    /**
     * 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. parseWithAdultCheck(URL url, int timeout)
  2. prettyPrint(String html)
  3. processHtml(String html)
  4. removeAllHtmlTags(String unsafe)
  5. removeHTMLTags(final String text)
  6. sanitizeHTML(String html)
  7. stripHTML(final String value)
  8. stripHtml(String html)
  9. tidyHtml(String html)