『壹』 用JSOUP解析HTML,怎樣刪除掉其中的一段DIV標簽及內容
用JSOUP解析HTML刪除掉其中的一段DIV標簽及內容的方法是根據id刪除。
使用核心方法如下:
doc.getElementById("detail_question").remove()
舉例如下:
從baseHtml這斷代碼中刪除指定id的標簽:
String baseHtml = "<div id='stylized' class='myform'>"
+ "<input id='txt_question' name='preg' type='text' disabled='disabled' style='width:150px;'>"
+ "<div id='detail_question'>Rock</div></div>";
Document doc = Jsoup.parse(baseHtml);
doc.getElementById("detail_question").remove();
Elements elements = doc.select("div");
System.out.println(elements);
輸出的結果中沒有detail_question這個div節點了:
<div id='stylized' class='myform'>
+ "<input id='txt_question' name='preg' type='text' disabled='disabled' style='width:150px;'>