2011-08-16 34 views
29

jsoup https://github.com/jhy/jsoup/pull/80'a xpath desteğinin eklenmesiyle ilgili bazı çalışmalar devam ediyor.jsoup xpath'i destekliyor mu?

  • Nasıl kullanabilirim?
+0

bu konu hakkında bir bilgi gemi dolusu orada yok: https://stackoverflow.com/questions/11816878/jsoup- css-selector-code-xpath-code dahil https://stackoverflow.com/questions/16335820/convert-xpath-to-jsoup-query https://stackoverflow.com/questions/11791596/how-to-get- mutlak-yol-of-bir-html-eleman https://groups.google.com/forum/?fromgroups#!topic/jsoup/lj4_-EJwH1Q –

cevap

11

JSoup henüz XPath desteklemez, ancak XSoup deneyebilirsiniz - "XPath ile Jsoup" .

Orada
@Test 
public void testSelect() { 

    String html = "<html><div><a href='https://github.com'>github.com</a></div>" + 
      "<table><tr><td>a</td><td>b</td></tr></table></html>"; 

    Document document = Jsoup.parse(html); 

    String result = Xsoup.compile("//a/@href").evaluate(document).get(); 
    Assert.assertEquals("https://github.com", result); 

    List<String> list = Xsoup.compile("//tr/td/text()").evaluate(document).list(); 
    Assert.assertEquals("a", list.get(0)); 
    Assert.assertEquals("b", list.get(1)); 
} 

da XSoup tarafından desteklenen özellikler ve XPath ifadeleri listesini bulabilirsiniz:

Burada projeler Github sitesinden (link) alıntı bir örnek. Henüz

1

değil, ama it.For örnek daha vardır JsoupXpath proje,

String xpath="//div[@id='post_list']/div[./div/div/span[@class='article_view']/a/num()>1000]/div/h3/allText()"; 
String doc = "..."; 
JXDocument jxDocument = new JXDocument(doc); 
List<Object> rs = jxDocument.sel(xpath); 
for (Object o:rs){ 
    if (o instanceof Element){ 
     int index = ((Element) o).siblingIndex(); 
     System.out.println(index); 
    } 
    System.out.println(o.toString()); 
}