I have this HTML table:
<table class="prk-fields">
<tbody>
<tr class="field_1 visibility-public field_type_textbox">
<td class="label">Date</td>
<td class="data">
<p>"2144"</p>
</td>
</tr>
<tr class="field_3 visibility-public alt field_type_textbox">
<td class="label">Location</td>
<td class="data">
<p>Planet Earth</p>
</td>
</tr>
<tr class="field_4 visibility-public field_type_url">
<td class="label">By</td>
<td class="data">
<p><a href="https://en.wikipedia.org/wiki/Extraterrestrial_life">Extraterrestrials</a>
</p>
</td>
</tr>
<tr class="field_5 visibility-public alt field_type_url">
<td class="label">Victims</td>
<td class="data">
<p>0</p>
</td>
</tr>
<tr class="field_6 visibility-public field_type_textbox">
<td class="label">Reaction</td>
<td class="data">
<p>Apathetic</p>
</td>
</tr>
<tr class="field_7 visibility-public alt field_type_textarea">
<td class="label">About</td>
<td class="data">
<p>it's about the 2144 attack on Earth by extraterrestrials</p>
</td>
</tr>
</tbody>
</table>
And I am parsing it with this code:
Document document = Jsoup.parse(response);
int index = 0;
for (Element td : document.select("td")) {
Log.d(TAG, "Row" + (++index));
for (Attribute attr : td.attributes()) {
Log.d(TAG, "TD " + attr.getKey() + " : " + attr.getValue());
}
for (Element p : td.select("p")) {
for (Attribute attr : td.attributes()) {
Log.d(TAG, "TTD " + attr.getKey() + " :: " + attr.getValue());
}
}
}
And what I am seeing in the logcat is:
Row1
TD class : label
Row2
TD class : data
TTD class :: data
Row3
TD class : label
Row4
TD class : data
TTD class :: data
Row5
TD class : label
Row6
TD class : data
TTD class :: data
but what I want is this:
Row1
TD Date : "2144"
Row2
TD Location : Planet Earth
Row3
TD By : Extraterrestrials
Row4
TD Victims : 0
Row5
TD Reaction : Apathetic
Row6
TD About : it's about the 2144 attack on Earth by extraterrestrials
Actually I don't have control over the number of rows but I know the columns are always two. And also the keys and values vary.
Please do you know how I could do it?
Try this you can change sysout
to Log
public class Test {
public static void main(String[] args) {
String response="<table class=\"prk-fields\">\n" +
" <tbody>\n" +
" <tr class=\"field_1 visibility-public field_type_textbox\">\n" +
" <td class=\"label\">Date</td>\n" +
" <td class=\"data\">\n" +
" <p>\"2144\"</p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr class=\"field_3 visibility-public alt field_type_textbox\">\n" +
" <td class=\"label\">Location</td>\n" +
" <td class=\"data\">\n" +
" <p>Planet Earth</p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr class=\"field_4 visibility-public field_type_url\">\n" +
" <td class=\"label\">By</td>\n" +
" <td class=\"data\">\n" +
" <p><a href=\"https://en.wikipedia.org/wiki/Extraterrestrial_life\">Extraterrestrials</a>\n" +
" </p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr class=\"field_5 visibility-public alt field_type_url\">\n" +
" <td class=\"label\">Victims</td>\n" +
" <td class=\"data\">\n" +
" <p>0</p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr class=\"field_6 visibility-public field_type_textbox\">\n" +
" <td class=\"label\">Reaction</td>\n" +
" <td class=\"data\">\n" +
" <p>Apathetic</p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr class=\"field_7 visibility-public alt field_type_textarea\">\n" +
" <td class=\"label\">About</td>\n" +
" <td class=\"data\">\n" +
" <p>it's about the 2144 attack on Earth by extraterrestrials</p>\n" +
" </td>\n" +
" </tr>\n" +
" </tbody>\n" +
"</table>";
Document document = Jsoup.parse(response);
int index=0;
for (Element table : document.select("table")) {
for (Element row : table.select("tr")) {
System.out.println("Row\t" + (++index));
Elements tds = row.select("td");
System.out.println("TD\t" +tds.get(0).text()+":"+tds.get(1).text());
}
}
}
}
Output:
Row 1
TD Date:"2144"
Row 2
TD Location:Planet Earth
Row 3
TD By:Extraterrestrials
Row 4
TD Victims:0
Row 5
TD Reaction:Apathetic
Row 6
TD About:it's about the 2144 attack on Earth by extraterrestrials
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.