@Test public void testGetSiblings() { Document doc = Jsoup.parse("<div><p>Hello<p id=1>there<p>this<p>is<p>an<p id=last>element</div>"); Element p = doc.getElementById("1"); assertEquals("there", p.text()); assertEquals("Hello", p.previousElementSibling().text()); assertEquals("this", p.nextElementSibling().text()); assertEquals("Hello", p.firstElementSibling().text()); assertEquals("element", p.lastElementSibling().text()); }
protected Void doInBackground(String... strings) { Elements moreInfo = new Elements(); try { Document pet = Jsoup.connect(strings[0]).get(); // Elements pics = pet.select("img[name=imgAnimalPhoto]"); Elements info = pet.select("table[id=detail-table]"); moreInfo = info.select("td"); for (Element x : moreInfo) { if (x.previousElementSibling().text().equals("Location:")) { // Nothing; Links are inconsisent. } else { labels.add(x.previousElementSibling().text()); data.add(x.text()); } } description = pet.select("[id = lbDescription]").text(); } catch (IOException e) { e.printStackTrace(); } return null; }
public JSONArray toFourDayJSON(String html, String[] labels) { Document doc = Jsoup.parse(html); JSONArray dates = new JSONArray(); try { Elements tables = doc.select("table"); // Log.d("jsoup", "Four day: Parsing html table: " + tables.size()); for (Element table : tables) { Elements rows = table.select("tr"); JSONObject date_item = new JSONObject(); JSONArray row_json = new JSONArray(); for (Element row : rows) { Elements data = row.select("td"); if (!data.isEmpty()) { JSONObject details = new JSONObject(); for (Element dataItem : data) { Elements img = dataItem.select("img"); String img_src = null, label = null; String[] tokens = null; if (img.size() == 0) { label = labels[data.indexOf(dataItem)]; if (label.equals("Time")) { details.put(label, dataItem.text().split("-")[0]); } else { details.put(label, dataItem.text()); } } else { img_src = img.get(0).attr("src"); tokens = img_src.split("/"); // Log.d("jsoup", "img: "+tokens[tokens.length-1]); details.put(labels[data.indexOf(dataItem)], tokens[tokens.length - 1]); } } row_json.put(details); } } date_item.put("data", new JSONArray(row_json.toString())); date_item.put("date", table.previousElementSibling().text()); dates.put(new JSONObject(date_item.toString())); } } catch (JSONException e) { e.printStackTrace(); } return dates; }