Java Code Examples for org.jsoup.nodes.Element#wrap()

The following examples show how to use org.jsoup.nodes.Element#wrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsoupTest.java    From crawler-jsoup-maven with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    
    String d = "<span><div>test</div></span>";
    Document doc = Jsoup.parse(d);
    Element div = doc.select("div").first(); // <div></div>
    div.html("<p>lorem ipsum</p>"); // <div><p>lorem ipsum</p></div>
    div.prepend("<p>First</p>");
    div.append("<p>Last</p>");
    // now: <div><p>First</p><p>lorem ipsum</p><p>Last</p></div>
    div.appendElement(d);
    Element span = doc.select("span").first(); // <span>One</span>
    span.wrap("<li><a href='http://example.com/'></a></li>");
    // now: <li><a href="http://example.com"><span>One</span></a></li>
    System.out.println(doc.html());
    
    String s = Jsoup.clean(doc.html(), "", Whitelist.relaxed(), new OutputSettings().prettyPrint(false));
    
    System.out.println(s);
}
 
Example 2
Source File: Elements.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 Wrap the supplied HTML around each matched elements. For example, with HTML
 {@code <p><b>This</b> is <b>Jsoup</b></p>},
 <code>doc.select("b").wrap("&lt;i&gt;&lt;/i&gt;");</code>
 becomes {@code <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>}
 @param html HTML to wrap around each element, e.g. {@code <div class="head"></div>}. Can be arbitrarily deep.
 @return this (for chaining)
 @see Element#wrap
 */
public Elements wrap(String html) {
    Validate.notEmpty(html);
    for (Element element : this) {
        element.wrap(html);
    }
    return this;
}
 
Example 3
Source File: Elements.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 Wrap the supplied HTML around each matched elements. For example, with HTML
 {@code <p><b>This</b> is <b>Jsoup</b></p>},
 <code>doc.select("b").wrap("&lt;i&gt;&lt;/i&gt;");</code>
 becomes {@code <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>}
 @param html HTML to wrap around each element, e.g. {@code <div class="head"></div>}. Can be arbitrarily deep.
 @return this (for chaining)
 @see Element#wrap
 */
public Elements wrap(String html) {
    Validate.notEmpty(html);
    for (Element element : this) {
        element.wrap(html);
    }
    return this;
}
 
Example 4
Source File: Elements.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 Wrap the supplied HTML around each matched elements. For example, with HTML
 {@code <p><b>This</b> is <b>Jsoup</b></p>},
 <code>doc.select("b").wrap("&lt;i&gt;&lt;/i&gt;");</code>
 becomes {@code <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>}
 @param html HTML to wrap around each element, e.g. {@code <div class="head"></div>}. Can be arbitrarily deep.
 @return this (for chaining)
 @see Element#wrap
 */
public Elements wrap(String html) {
    Validate.notEmpty(html);
    for (Element element : this) {
        element.wrap(html);
    }
    return this;
}
 
Example 5
Source File: Elements.java    From jsoup-learning with MIT License 3 votes vote down vote up
/**
 Wrap the supplied HTML around each matched elements. For example, with HTML
 {@code <p><b>This</b> is <b>Jsoup</b></p>},
 <code>doc.select("b").wrap("&lt;i&gt;&lt;/i&gt;");</code>
 becomes {@code <p><i><b>This</b></i> is <i><b>jsoup</b></i></p>}
 @param html HTML to wrap around each element, e.g. {@code <div class="head"></div>}. Can be arbitrarily deep.
 @return this (for chaining)
 @see Element#wrap
 */
public Elements wrap(String html) {
    Validate.notEmpty(html);
    for (Element element : contents) {
        element.wrap(html);
    }
    return this;
}