2015年10月25日 星期日

[ Java 代碼範本 ] jsoup - Extract attributes, text, and HTML from elements

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

Solution 
* To get the value of an attribute, use the Node.attr(String key) method
* For the text on an element (and its combined children), use Element.text()
* For HTML, use Element.html(), or Node.outerHtml() as appropriate

For example: 
  1. String html = "An example link.
    "
    ;  
  2. Document doc = Jsoup.parse(html);  
  3. Element link = doc.select("a").first();  
  4.   
  5. String text = doc.body().text(); // "An example link"  
  6. String linkHref = link.attr("href"); // "http://example.com/"  
  7. String linkText = link.text(); // "example""  
  8.   
  9. String linkOuterH = link.outerHtml();   
  10.     // "example"  
  11. String linkInnerH = link.html(); // "example"  
Description 
The methods above are the core of the element data access methods. There are additional others: 
* Element.id()
* Element.tagName()
* Element.className() and Element.hasClass(String className)

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

See also 
* The reference documentation for Element and the collection Elements class 
Working with URLs 
finding elements with the CSS selector syntax

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...