2016-08-16 216 views
0

如何選擇第一個div標籤。如果我使用div> *它選擇所有div並且我只想要第一個div內的標籤。請協助。如何在嵌套的div標籤中選擇第一個div標籤

String htmlString = 
"<html><div><div><div><p><b>This is first line</b></p> </div><b>This is second line</b></div><div><span style=\"color:blue\">This is third line</span></div></html>"`; 

      org.jsoup.nodes.Document doc1 = Jsoup.parse(htmlString); 

      Elements elements1 = doc1.select("html:first-child"); 

      for (Element element : elements1) { 
       System.out.println(element.toString()); 
      } 

我想要的輸出,

<div> 
    <div> 
    <p><b>This is first line</b></p> 
    </div> 
    <b>This is second line</b> </div> <div> 
    <span style="color:blue">This is third line</span> </div> 

回答

0

您可以使用HTML()來得到一個div內部

String result = doc1.select("div").first().html(); 
System.out.println(result);