2017-10-19 3 views
-1

Jsoup를 사용하여 HTML 문자열에서 데이터를 가져 오려고했지만 성공하지 못했습니다. HTML 코드Jsoup은 항상 null을 반환합니다.

<form> 
<table> 
    <tr> 
     <th>First</th> 
     <th>Second</th> 
     <th>Third</th> 
    </tr> 
    <tr> 
     <td><input type="text" name="elems[][f]" value="one" /></td> 
     <td><input type="text" name="people[][s]" value="two" /></td> 
     <td><input type="text" name="people[][t]" value="three" /></td> 
    </tr> 
    <tr> 
     <td><input type="text" name="elems[][f]" value="one1" /></td> 
     <td><input type="text" name="people[][s]" value="two2" /></td> 
     <td><input type="text" name="people[][t]" value="three3" /></td> 
    </tr> 
</table> 
<input type="submit" value="next" /> 
내가 다른 케이스를 시도했습니다

, 불과하다. 나는 th와 모든 td에서 데이터를 검색 할 것이다. ...이 같은 문자열 변수로 HTML 입력을 배치하면

Document document = Jsoup.parse(HTMLSTring); 

    Elements tables = document.select("table"); 
    for (Element table : tables){ 
     Elements ths = table.getElementsByTag("th"); 
     for(Element th : ths) 
     { 
      System.out.println(th.text()); 
     } 
    } 
+0

먼저 'tr'을 확인할 필요가 없습니까? –

+0

올바른 * '

'을 (를) 만들려고하십니까? ''요소 안에있는 ''요소 안에 있습니까? 그리고 ''엘리먼트로 grandfathered되는 곳은 어디입니까? – EJP

+0

'getElementsByTag'가 반복적이기 때문에'tr'을 먼저 검사 할 필요가 없습니다. 코드가 작동합니다 ... JSoup이 'null'을 반환하는 것이 아닙니다. 'HTMLSTring'에 올바른 html 콘텐츠를 받았는지 확인 했습니까? – Nirekin

답변

1

귀하의 코드가 작동 :

public static void main(String[] args) { 
    String HTMLSTring= "<form><table><tr><th>First</th><th>Second</th><th>Third</th></tr><tr><td><input type=\"text\" name=\"elems[][f]\" value=\"one\" /></td><td><input type=\"text\" name=\"people[][s]\" value=\"two\" /></td><td><input type=\"text\" name=\"people[][t]\" value=\"three\" /></td></tr><tr><td><input type=\"text\" name=\"elems[][f]\" value=\"one1\" /></td><td><input type=\"text\" name=\"people[][s]\" value=\"two2\" /></td><td><input type=\"text\" name=\"people[][t]\" value=\"three3\" /></td></tr></table><input type=\"submit\" value=\"next\" />"; 

    Document document = Jsoup.parse(HTMLSTring); 
    Elements tables = document.select("table"); 
    for (Element table : tables){ 
     Elements ths = table.getElementsByTag("th"); 
     for(Element th : ths) 
     { 
      System.out.println(th.text()); 
     } 
    } 
} 

은 당신이 예상 한 것이 "일"에 대한 예입니다 결과.

  • 당신은 당신이 HTML 입력을 읽고있는 방법으로 문제가있을 수 있습니다
  • 둘째
  • 셋째

첫째.

예를 들어, 에 중단 점을 입력하여 올바른 내용을 수신해야합니다.

'요소가 '
'엘리먼트가 '