1

How to purse a whole table using jsoup into Android?

let input:

<table>
<tr>
<th>name</th>
<th>age</th>
</tr>
<tr>
<td>john doe</td>
<td>25</td>
</tr>
<tr>
<td>xxx yyy </td>
<td>28</td>
</tr>

output: name age john doe 25 xxx yyy 28 .Btw, you won't have to take the input manually. I need to purse the table from my website.

Tazwar Utshas
  • 921
  • 2
  • 17
  • 30

2 Answers2

1

Now i can't try it, but you can use somethins like this:

Elements ths = document.select("table tr th");
ths.html()

Elements tds = document.select("table tr td");
tds.html()

I don't know returned strings format, if they are separated by some spaces or not, but you can try.

If you want to manage separately tds output you can iterate over Elements and get single html values

firegloves
  • 5,581
  • 2
  • 29
  • 50
  • Well bro, Your answer is looking promising, but somehow, it is not working. Listen, string format doesn't matter. I will be able to handle it. but, I can't get the string at all... Can you plz tell me what should I do to get all the elements of a specific table? Say, the table has a class named "special_table". – Tazwar Utshas Nov 13 '16 at 02:12
1

You can use the text() method of Element:

final String html = "<table>\n"
        + "<tr>\n"
        + "<th>name</th>\n"
        + "<th>age</th>\n"
        + "</tr>\n"
        + "<tr>\n"
        + "<td>john doe</td>\n"
        + "<td>25</td>\n"
        + "</tr>\n"
        + "<tr>\n"
        + "<td>xxx yyy </td>\n"
        + "<td>28</td>\n"
        + "</tr>";

Document doc = Jsoup.parse(html);
Element table = doc.select("table").first(); // Take first 'table' found

System.out.println(table.text()); // Print result

Output:

name age john doe 25 xxx yyy 28

If you have more than one table:

for( Element e : doc.select("table") )
{
    System.out.println(e.text());
}

Or since JDK8:

doc.select("table").forEach((e) ->
{
    System.out.println(e.text());
});

Getting JS content (see comments)

Community
  • 1
  • 1
ollo
  • 24,797
  • 14
  • 106
  • 155
  • Sorry bro, for my wrong format of question. Actually you won't have to take that input manually. Say, there is a table in this website. http://www.espncricinfo.com/ci/engine/match/1034809.html . What will you do to purse that table? Your answer was looking promising enough and that's why I thought you could help me perfectly :) – Tazwar Utshas Nov 13 '16 at 02:17
  • Parsing a website instead of a string isn't a big deal, just replace `Jsoup.parse(...)` with `Jsoup.connect("your url here").get()`. This will connect to the website, parse it's content and you can continue as usual – timeout, user agent, login data, cookies etc. can be set too. – ollo Nov 13 '16 at 14:47
  • A problem I see with the page you mentioned: It relies heavy on Javascript, eg. there's no plain html for the tables – and Jsoup can't handle JS. But there are solutions for this too, I'll edit some ways to do so into my answer. – ollo Nov 13 '16 at 14:48
  • Btw. if your question is solved, please *accept* the answer that solved your problem. – ollo Nov 19 '16 at 16:58