@Test public void testHashAndEqualsAndValue() { // .equals and hashcode are identity. value is content. String doc1 = "<div id=1><p class=one>One</p><p class=one>One</p><p class=one>Two</p><p class=two>One</p></div>" + "<div id=2><p class=one>One</p><p class=one>One</p><p class=one>Two</p><p class=two>One</p></div>"; Document doc = Jsoup.parse(doc1); Elements els = doc.select("p"); /* for (Element el : els) { System.out.println(el.hashCode() + " - " + el.outerHtml()); } 0 1534787905 - <p class="one">One</p> 1 1534787905 - <p class="one">One</p> 2 1539683239 - <p class="one">Two</p> 3 1535455211 - <p class="two">One</p> 4 1534787905 - <p class="one">One</p> 5 1534787905 - <p class="one">One</p> 6 1539683239 - <p class="one">Two</p> 7 1535455211 - <p class="two">One</p> */ assertEquals(8, els.size()); Element e0 = els.get(0); Element e1 = els.get(1); Element e2 = els.get(2); Element e3 = els.get(3); Element e4 = els.get(4); Element e5 = els.get(5); Element e6 = els.get(6); Element e7 = els.get(7); assertEquals(e0, e0); assertTrue(e0.hasSameValue(e1)); assertTrue(e0.hasSameValue(e4)); assertTrue(e0.hasSameValue(e5)); assertFalse(e0.equals(e2)); assertFalse(e0.hasSameValue(e2)); assertFalse(e0.hasSameValue(e3)); assertFalse(e0.hasSameValue(e6)); assertFalse(e0.hasSameValue(e7)); assertEquals(e0.hashCode(), e0.hashCode()); assertFalse(e0.hashCode() == (e2.hashCode())); assertFalse(e0.hashCode() == (e3).hashCode()); assertFalse(e0.hashCode() == (e6).hashCode()); assertFalse(e0.hashCode() == (e7).hashCode()); }
private void getChildElement(Element parentElement, Integer level) { parentElement.html(deleteComent(parentElement.html())); // System.out.println("key:"+(level+","+parentElement.hashCode())+",value:"+parentElement.html()); if (parentElement.children().size() > 0) { level += 1; for (int i = 0; i < parentElement.children().size(); i++) { if (("ul".equals(parentElement.tagName().toLowerCase())) || ("table".equals(parentElement.tagName().toLowerCase()))) { // 整体标签 String html = parentElement.html().replaceAll(" ", "").replaceAll(" ", ""); // 去中英文空格 if (html.contains("首页") || parentElement.id().contains("nav")) { // // System.out.println("----------------------首页Start-----------------------------"); Elements links = parentElement.select("a"); for (Element ele : links) { if (topMenumap.get(level + "," + ele.hashCode()) == null) { topMenumap.put(level + "," + ele.hashCode(), ele); // System.out.println(level + "," + ele.hashCode() + ",---------------" + // ele.html()); // System.out.println("a:" + // ele.attr("abs:href") + ",文本:" + ele.text()); } } // System.out.println("----------------------首页End-----------------------------"); } else { map.put(level + "," + parentElement.hashCode(), parentElement); } } else { getChildElement(parentElement.child(i), level); } } } else { if ("script".equals(parentElement.tagName().toLowerCase())) { return; } if (StringUtils.isNotEmpty(parentElement.html())) { level += 1; map.put(level + "," + parentElement.hashCode(), parentElement); } } }