2011-08-16 2 views
29

jsoup에 xpath 지원을 추가하는 것과 관련된 몇 가지 작업이 있습니다 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

아니지만, 클라운 예을 가지고 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()); 
} 
관련 문제