Class Jsoup
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringGet safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a safe-list of permitted tags and attributes.static StringDeprecated.as of 1.14.1.static StringGet safe HTML from untrusted input HTML, by parsing input HTML and filtering it through an allow-list of safe tags and attributes.static Stringclean(String bodyHtml, String baseUri, Safelist safelist, Document.OutputSettings outputSettings) Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a safe-list of permitted tags and attributes.static StringDeprecated.as of 1.14.1.static Stringclean(String bodyHtml, String baseUri, Whitelist safelist, Document.OutputSettings outputSettings) Deprecated.as of 1.14.1.static booleanTest if the input body HTML has only tags and attributes allowed by the Safelist.static booleanDeprecated.as of 1.14.1.static DocumentParse the contents of a file as HTML.static DocumentParse the contents of a file as HTML.static Documentparse(InputStream in, String charsetName, String baseUri) Read an input stream, and parse it to a Document.static Documentparse(InputStream in, String charsetName, String baseUri, Parser parser) Read an input stream, and parse it to a Document.static DocumentParse HTML into a Document.static DocumentParse HTML into a Document.static DocumentParse HTML into a Document, using the provided Parser.static DocumentparseBodyFragment(String bodyHtml) Parse a fragment of HTML, with the assumption that it forms thebodyof the HTML.static DocumentparseBodyFragment(String bodyHtml, String baseUri) Parse a fragment of HTML, with the assumption that it forms thebodyof the HTML.
-
Constructor Details
-
Jsoup
private Jsoup()
-
-
Method Details
-
parse
Parse HTML into a Document. The parser will make a sensible, balanced document tree out of any HTML.- Parameters:
html- HTML to parsebaseUri- The URL where the HTML was retrieved from. Used to resolve relative URLs to absolute URLs, that occur before the HTML declares a<base href>tag.- Returns:
- sane HTML
-
parse
Parse HTML into a Document, using the provided Parser. You can provide an alternate parser, such as a simple XML (non-HTML) parser.- Parameters:
html- HTML to parsebaseUri- The URL where the HTML was retrieved from. Used to resolve relative URLs to absolute URLs, that occur before the HTML declares a<base href>tag.parser- alternateparserto use.- Returns:
- sane HTML
-
parse
-
parse
Parse the contents of a file as HTML.- Parameters:
in- file to load HTML fromcharsetName- (optional) character set of file contents. Set tonullto determine fromhttp-equivmeta tag, if present, or fall back toUTF-8(which is often safe to do).baseUri- The URL where the HTML was retrieved from, to resolve relative links against.- Returns:
- sane HTML
- Throws:
IOException- if the file could not be found, or read, or if the charsetName is invalid.
-
parse
Parse the contents of a file as HTML. The location of the file is used as the base URI to qualify relative URLs.- Parameters:
in- file to load HTML fromcharsetName- (optional) character set of file contents. Set tonullto determine fromhttp-equivmeta tag, if present, or fall back toUTF-8(which is often safe to do).- Returns:
- sane HTML
- Throws:
IOException- if the file could not be found, or read, or if the charsetName is invalid.- See Also:
-
parse
Read an input stream, and parse it to a Document.- Parameters:
in- input stream to read. Make sure to close it after parsing.charsetName- (optional) character set of file contents. Set tonullto determine fromhttp-equivmeta tag, if present, or fall back toUTF-8(which is often safe to do).baseUri- The URL where the HTML was retrieved from, to resolve relative links against.- Returns:
- sane HTML
- Throws:
IOException- if the file could not be found, or read, or if the charsetName is invalid.
-
parse
public static Document parse(InputStream in, String charsetName, String baseUri, Parser parser) throws IOException Read an input stream, and parse it to a Document. You can provide an alternate parser, such as a simple XML (non-HTML) parser.- Parameters:
in- input stream to read. Make sure to close it after parsing.charsetName- (optional) character set of file contents. Set tonullto determine fromhttp-equivmeta tag, if present, or fall back toUTF-8(which is often safe to do).baseUri- The URL where the HTML was retrieved from, to resolve relative links against.parser- alternateparserto use.- Returns:
- sane HTML
- Throws:
IOException- if the file could not be found, or read, or if the charsetName is invalid.
-
parseBodyFragment
-
parseBodyFragment
-
clean
Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through an allow-list of safe tags and attributes.- Parameters:
bodyHtml- input untrusted HTML (body fragment)baseUri- URL to resolve relative URLs againstsafelist- list of permitted HTML elements- Returns:
- safe HTML (body fragment)
- See Also:
-
clean
Deprecated.as of 1.14.1.Useclean(String, String, Safelist)instead. -
clean
Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a safe-list of permitted tags and attributes.Note that as this method does not take a base href URL to resolve attributes with relative URLs against, those URLs will be removed, unless the input HTML contains a
<base href> tag. If you wish to preserve those, use theclean(String html, String baseHref, Safelist)method instead, and enable}.invalid @link
{@link Safelist#preserveRelativeLinks(boolean true)- Parameters:
bodyHtml- input untrusted HTML (body fragment)safelist- list of permitted HTML elements- Returns:
- safe HTML (body fragment)
- See Also:
-
clean
Deprecated.as of 1.14.1.Useclean(String, Safelist)instead. -
clean
public static String clean(String bodyHtml, String baseUri, Safelist safelist, Document.OutputSettings outputSettings) Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a safe-list of permitted tags and attributes.The HTML is treated as a body fragment; it's expected the cleaned HTML will be used within the body of an existing document. If you want to clean full documents, use
Cleaner.clean(Document)instead, and add structural tags (html, head, bodyetc) to the safelist.- Parameters:
bodyHtml- input untrusted HTML (body fragment)baseUri- URL to resolve relative URLs againstsafelist- list of permitted HTML elementsoutputSettings- document output settings; use to control pretty-printing and entity escape modes- Returns:
- safe HTML (body fragment)
- See Also:
-
clean
@Deprecated public static String clean(String bodyHtml, String baseUri, Whitelist safelist, Document.OutputSettings outputSettings) Deprecated.as of 1.14.1. -
isValid
Test if the input body HTML has only tags and attributes allowed by the Safelist. Useful for form validation.The input HTML should still be run through the cleaner to set up enforced attributes, and to tidy the output.
Assumes the HTML is a body fragment (i.e. will be used in an existing HTML document body.)
- Parameters:
bodyHtml- HTML to testsafelist- safelist to test against- Returns:
- true if no tags or attributes were removed; false otherwise
- See Also:
-
isValid
Deprecated.as of 1.14.1.UseisValid(String, Safelist)instead.
-