Tazwar Utshas
Tazwar Utshas

Reputation: 961

Pursing table using jsoup

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.

Upvotes: 1

Views: 102

Answers (2)

ollo
ollo

Reputation: 25380

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)

Upvotes: 1

firegloves
firegloves

Reputation: 5709

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

Upvotes: 1

Related Questions