2013-07-17 2 views
0

아래 html의 span 태그 다음에 다음 노드 값 (4 번)을 선택하려고합니다. 어떻게 할 수 있습니까 ??특정 조건 다음 노드 선택

<tr valign="top"> 
    <td></td> 
    <td><a href="#"> 1 </a></td> 
    <td><a href="#"> 2 </a></td> 
    <td><span> 3 </span></td> 
    <td><a href="#"> 4 </a></td> 
    <td><a href="#"> 5 </a></td> 
    <td><a href="#"> 6 </a></td> 
</tr> 

답변

0
final String html = "<tr valign=\"top\">\n" 
     + " <td></td>\n" 
     + " <td><a href=\"#\"> 1 </a></td>\n" 
     + " <td><a href=\"#\"> 2 </a></td>\n" 
     + " <td><span> 3 </span></td>\n" 
     + " <td><a href=\"#\"> 4 </a></td>\n" 
     + " <td><a href=\"#\"> 5 </a></td>\n" 
     + " <td><a href=\"#\"> 6 </a></td>\n" 
     + "</tr>"; 

Document doc = Jsoup.parse(html); 

Element nextToSpan = doc.select("span").first().nextElementSibling(); 

는 설명 :

doc.select("span") // Select the span-tags of doc 
    .first() // retrieve the first one 
    .nextElementSibling(); // Get the element that's next to it 

문서 :http://jsoup.org/cookbook/extracting-data/selector-syntax

+0

TKS는 ... 작동! – adrianogf