2011-08-16 11 views
29

xpathサポートをjsoup https://github.com/jhy/jsoup/pull/80に追加する作業が進行中です。jsoupはxpathをサポートしていますか?

  • それは機能していますか?
  • どうすれば使用できますか?
+0

このトピックに関する情報の最大積載量はそこにあります。https://stackoverflow.com/questions/11816878/jsoup- css-selector-code-xpath-code-included https://stackoverflow.com/questions/16335820/convert-xpath-to-jsoup-query https://stackoverflow.com/questions/11791596/how-to-get-絶対パスのHTML要素https://groups.google.com/forum/?fromgroups#!topic/jsoup/lj4_-EJwH1Q –

答えて

11

JSoupはまだのXPathをサポートしていませんが、あなたはXSoup試すこと - "のXPathでJsoup" を。

あり
@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)); 
} 

あなたもXSoupによってサポートされている機能およびXPathの表現のリストを見つけることができます:

はここで、プロジェクトのGithubサイト(link)から引用した例です。

1

まだ、しかしit.For例を作る持ってJsoupXpathプロジェクト、

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()); 
} 
関連する問題