我刚刚开始学习jsoup,我正在制定简单的日程安排,而且我有以下问题。我希望按日期排序结果(当列表的每个项目包括日期,时间和标题时)
html代码示例:
<table class="calendar_table calendar_table_agenda">
<tbody><tr>
<td class="agenda-date " rowspan="22">
Pn, 25-05-2015
</td>
<td class="agenda-time">
06:30
-
07:25 </td>
<td class="agenda-title ">
Class monday </td>
</tr>
<tr>
<td class="agenda-time">
06:30
-
07:25</td>
<td class="agenda-title ">
Class monday </td>
</tr>
<tr>
<td class="agenda-time">
07:30
-
08:25 </td>
<td class="agenda-title ">
Class monday </td>
</tr>
<tr>
<td class="agenda-time">
07:30
-
08:25 </td>
<td class="agenda-title ">
Class monday
</td>
</tr>
<tr>
<td class="agenda-date " rowspan="22">
Wt, 26-05-2015 </td>
<td class="agenda-time">
06:30
-
07:25
</td>
<td class="agenda-title ">
Class tuesday </td>
</tr>
<tr>
<td class="agenda-time">
06:30
-
07:25
</td>
<td class="agenda-title ">
Class tuesday
</td>
</tr>
<tr>
<td class="agenda-date current" rowspan="22">
Śr, 27-05-2015
</td>
<td class="agenda-time">
06:30
-
07:25 </td>
<td class="agenda-title ">
Class wensday </td>
</tr>
以下代码只给出了我每天的第一个结果(标题+时间+日期),而不是全部:
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
int index = 0;
try {
Document doc = Jsoup.connect(url).get();
for (Element table : doc.select("table[class=calendar_table calendar_table_agenda]")) {
for (Element row : table.select("tr:gt(0)")) {
HashMap<String, String> map = new HashMap<String, String>();
dateArray = new String[]{pon, wt, sroda, czwt, pt, sb};
for (Element date : row.getElementsByClass("agenda-date")){
if(date.nextElementSibling()!=null) {
Elements classes = row.getElementsByClass("agenda-title");
Elements time = row.getElementsByClass("agenda-time");
map.put("classes", classes.get(0).text());
map.put("time", time.get(0).text());
map.put("date", date.text());
arraylist.add(map);
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
我想得到的是:
第一天的结果
第二天的结果
...
第2天第一个结果
第二天第二次结果
...
我现在得到了
第一天的结果
第2天第一个结果
感谢您的帮助!
答案 0 :(得分:0)
要获取所有行,请改用此CSS选择器:
table.calendar_table.calendar_table_agenda tr:gt(0)
您可以删除一个for
循环并使用以下代码:
for (Element row : doc.select("table.calendar_table.calendar_table_agenda tr:gt(0)")) {
HashMap<String, String> map = new HashMap<String, String>();
dateArray = new String[]{pon, wt, sroda, czwt, pt, sb};
for (Element date : row.getElementsByClass("agenda-date")){
if(date.nextElementSibling()!=null) {
Elements classes = row.getElementsByClass("agenda-title");
Elements time = row.getElementsByClass("agenda-time");
map.put("classes", classes.get(0).text());
map.put("time", time.get(0).text());
map.put("date", date.text());
arraylist.add(map);
}
}
}