分享

8. Extract attributes, text, and HTML from elements

 goldbomb 2010-10-08

Extract attributes, text, and HTML from elements

Problem

After parsing a document, and finding some elements, you'll want to get at the data inside those elements.

Solution

For example:

String html = "<p>An <a href='http:///'><b>example</b></a> link.</p>"; 
Document doc = Jsoup.parse(html);Element link = doc.select("a").first(); 
String text = doc.body().text(); // "An example link" 
String linkHref = link.attr("href"); // "http:///" 
String linkText = link.text(); // "example"" 
String linkOuterH = link.outerHtml();  
   
// "<a href="http://"><b>example</b></a>" 
String linkInnerH = link.html(); // "<b>example</b>"

Description

The methods above are the core of the element data access methods. There are additional others:

All of these accessor methods have corresponding setter methods to change the data.

See also

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多