public class HtmlUtil extends Object
Jsoup
library newOutputSetting(Document.OutputSettings)
The easiest way to convert Element
, Elements
to String
is toString()
method
Constructor and Description |
---|
HtmlUtil() |
Modifier and Type | Method and Description |
---|---|
static Element |
coverTag(String html,
String... tagNames)
cover
html by tagNames by first element of tagName will be outermost. |
static Elements |
getHtmlInTag(String html,
String tag)
get html tag include tag too
|
static void |
newOutputSetting(Document.OutputSettings setting)
setting new output.
|
static Document |
parse(String html)
convert html String to
Document (A lot more easier to manage it) |
static String |
removeTag(Elements html)
remove top tag and return as string
The output, can managing by newOutputSetting(Document.OutputSettings) |
static String |
removeTag(String html)
remove top tag and return as string
|
static Element |
toHtmlByHtml(String html)
insert html tag in the top
Beware: It's include head and body tag too |
static Element |
toHtmlByPlain(String plainText)
same work with
toHtmlByHtml(String) but think input parameter as plain text (so meaning if there have charactor that cannot convert to html it's will change to other) Example: |
public static void newOutputSetting(Document.OutputSettings setting)
setting
- Document.OutputSettings
Document.OutputSettings
public static Elements getHtmlInTag(String html, String tag)
input (html): <div><code>Hello world</code>!</div>
input (tag): code
output: <code>Hello world</code>
so you can remove the input tag by using removeTag(String)
html
- searching htmltag
- tagName (learn more Tag.valueOf(String)
)Elements
(which contains of html string inside tag parameter)removeTag(String)
public static String removeTag(String html)
input: <div>I am <code>Java</code> programmer</div>
output: I am <code>Java</code> programmer
The output, can managing by newOutputSetting(Document.OutputSettings)
html
- input htmlpublic static String removeTag(Elements html)
newOutputSetting(Document.OutputSettings)
html
- input Elements (easy get from getHtmlInTag(String, String)
)removeTag(String)
public static Element toHtmlByHtml(String html)
input: <div>I am <code>Java</code> programmer</div>
output:
<html>
<head></head>
<body>
<div>I am <code>Java</code> programmer</div>
</body>
</html>
The output, can managing by newOutputSetting(Document.OutputSettings)
html
- input htmlpublic static Element toHtmlByPlain(String plainText)
toHtmlByHtml(String)
but think input parameter as plain text (so meaning if there have charactor that cannot convert to html it's will change to other)
input: <div>I am <code>Java</code> programmer</div>
output:
<html>
<head></head>
<body>
<div>I am <code>Java</code> programmer</div>
</body>
</html>
plainText
- plain textpublic static Element coverTag(String html, String... tagNames)
html
by tagNames
by first element of tagName will be outermost.
input (html): hello world
input (tags): [html, body, div]
output:
<html>
<body>
<div>hello world</div>
</body>
</html>
html
- input htmltagNames
- multiple tags namepublic static Document parse(String html)
Document
(A lot more easier to manage it)html
- input htmlDocument
,
Document.head()
,
Document.body()
Copyright © 2017. All rights reserved.