jsoup-Data modification

Source: Internet
Author: User

After you parse a document, you may want to modify some of the property values, and then save to disk or output to the foreground page.

You can use properties to set methods Element.attr(String key, String value) , and Elements.attr(String key, String value) .

If you need to modify the attributes of an element class , you can use the Element.addClass(String className) and Element.removeClass(String className) methods.

ElementsProvides methods for bulk manipulation of element attributes and classes, such as: To add one to each a element in a div, rel="nofollow" you can use the following methods:

    @Test    publicvoid  setAttr () {        = "<div class=comments><a >1</a><a>2</a></div> ";         =Jsoup.parse (HTML);        Doc.select ("div.comments a"). attr ("rel", "nofollow");        System.out.println (DOC);    }

Elementas with the other methods in, the attr method is returned when Element (or when a selector is used to return a Elements collection). This makes it easy to use the method used in writing. Like what:

doc.select("div.masthead").attr("title", "jsoup").addClass("round-box");
Set the HTML content of an element

You need the HTML content in an element

You can use Element the HTML settings in the following ways:

@Test Public voidsethtml () {String HTML= "<span></span><div class=comments><a>1</a><a>2</a></div>"; Document Doc=jsoup.parse (HTML); 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>");//Add HTML content in front of DivDiv.append ("<p>Last</p>");//Add HTML content after DivSystem.out.println (DOC); Element span= Doc.select ("span"). First ();//<span>One</span>Span.wrap ("<li><a href= ' http://example.com/' ></a></li>");    System.out.println (DOC); }
    • Element.html(String html)This method clears the HTML content from the element and then replaces it with the incoming HTML.
    • Element.prepend(String first)And Element.append(String last) methods are used to add HTML content before and after the HTML inside the element, respectively
    • Element.wrap(String around)Wraps an external HTML content on the element.
Set the text content of an element

You need to modify the text content in an HTML document

You can use Element the Setup method:

    @Test    publicvoid  SetText () {        = "<span></span><div Class=comments><a>1</a><a>2</a></div> ";         =Jsoup.parse (HTML);         // <div></div>        Div.text ("5 > 4");        Div.prepend ("First");        Div.append ("last");        System.out.println (div);    }

The text setting method is the same as the HTML setter method:

    • Element.text(String text)Clears the inner HTML content of an element, and then provides the text instead
    • Element.prepend(String first)and Element.append(String last) add text nodes before and after the inner HTML of the element.

For incoming text, if it contains characters such as, and so on, < > it will be processed in text, not HTML.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.