Reputation: 961
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
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
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());
});
Upvotes: 1
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