Extract attributes, text, and HTML from elements

Source: Internet
Author: User

After parsing a Document Instance Object and finding some elements, you want to obtain the data in these elements. To obtain an attribute value, you can use Node. attr (String key) values () or Node. outerHtml () as an example:

String html = "<p> An <a href = 'HTTP: // example.com/'> <B> example </B> </a> link. </p> "; Document doc = Jsoup. parse (html); // parse the HTML string and return a Document to implement Element link = doc. select (""). first (); // search for the first a element String text = doc. body (). text (); // "An example link" // obtain the text String linkHref = link in the String. attr ("href"); // "http://example.com/" // get link address String linkText = link. text (); // "example" "// obtain the text String linkOuterH = link in the link address. outerHtml (); // "<a href =" http://example.com "> <B> example </B> </a>" String linkInnerH = link.html (); // "<B> example </B>" // obtain the html content in the link.

 

The preceding method is the core method for element data access. In addition, you can use Element. id () Element. tagName () Element. className () and Element. hasClass (String className) All these accessors have corresponding setter methods to change data.

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.