Here you can find the source of coverTag(String html, String... tagNames)
Parameter | Description |
---|---|
html | input html |
tagNames | multiple tags name |
public static Element coverTag(String html, String... tagNames)
//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; } }