tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Jsoup > Selection Example3

Selection Example3 

Jsoup is an open source java library for parsing and manipulating HTML with ease. Get the latest binaries from http://jsoup.org/. This requires the library jsoup-1.6.1.jar to be in classpath. The following example shows div selection.

File Name  :  
/JSOUP001/config/html/Simple.html 
01<html>
02<head>
03<title>BE THE CODER</title>
04<script type="text/javascript" src="jQuery.js"></script>
05<link rel="shortcut icon" href="http://bethecoder.com/favicon.ico">
06<link rel="apple-touch-icon" href="http://bethecoder.com/apple-touch-icon.png">
07<script type="text/javascript" src="Tooltip.js"></script>
08</head>
09<body>
10    <div id="notify-container">Notify Content</div>
11    <div id="overlay-header">Overlay Header Content</div>
12    <div id="custom-header">Custom Header Content</div>
13     
14    <div class="container">
15     Container Data Content <a id="nav-cont" href="/content">AAA</a> BBB CCC DDDD
16    </div>
17     
18    <div class="nav mainnavs">
19        <ul>
20            <li class="youarehere"><a id="nav-questions" href="/questions">Questions</a></li>
21            <li><a id="nav-tags" href="/tags">Tags</a></li>
22            <li><a id="nav-users" href="/users">Users</a></li>
23        </ul>
24    </div>
25    <div id="footer-one">Custom Footer Content1</div>
26    <div id="footer-two">Custom Footer Content2</div>
27</body>
28</html>

File Name  :  
com/bethecoder/tutorials/jsoup/tests/SelectTest3.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.jsoup.tests;

import java.io.IOException;
import java.io.InputStream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class SelectTest3 {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {
    InputStream ins = SelectTest3.class.
        getClassLoader().getResourceAsStream("Simple.html");
    
    Document doc = Jsoup.parse(ins, "UTF-8""btc.com");
    System.out.println(doc.select("div[id$=header]"))//div id ending with header
    System.out.println(doc.select("div[id^=footer]"))//div id starting with header
    System.out.println(doc.select("div[id]"));       //divs having id as attribute
  }

}
   

It gives the following output,
File Name  :  OUTPUT
01<div id="overlay-header">
02 Overlay Header Content
03</div>
04<div id="custom-header">
05 Custom Header Content
06</div>
07 
08 
09<div id="footer-one">
10 Custom Footer Content1
11</div>
12<div id="footer-two">
13 Custom Footer Content2
14</div>
15 
16 
17<div id="notify-container">
18 Notify Content
19</div>
20<div id="overlay-header">
21 Overlay Header Content
22</div>
23<div id="custom-header">
24 Custom Header Content
25</div>
26<div id="footer-one">
27 Custom Footer Content1
28</div>
29<div id="footer-two">
30 Custom Footer Content2
31</div>



 
  


  
bl  br