5
A
回答
7
是的,它是可能的。這是解決這個的一種方法:
- 找到所有註釋節點
- 對於每個註釋中提取數據屬性
- 插入一個新的節點與數據當前註釋節點
- 後刪除註釋節點
看一看這段代碼:
public class UncommentComments {
public static void main(String... args) {
String htmlIn = "<html><head></head><body>"
+ "<!--<div> hello there </div>-->"
+ "<div>not a comment</div>"
+ "<!-- <h5>another comment</h5> -->"
+ "</body></html>";
Document doc = Jsoup.parse(htmlIn);
List<Comment> comments = findAllComments(doc);
for (Comment comment : comments) {
String data = comment.getData();
comment.after(data);
comment.remove();
}
System.out.println(doc.toString());
}
public static List<Comment> findAllComments(Document doc) {
List<Comment> comments = new ArrayList<>();
for (Element element : doc.getAllElements()) {
for (Node n : element.childNodes()) {
if (n.nodeName().equals("#comment")){
comments.add((Comment)n);
}
}
}
return Collections.unmodifiableList(comments);
}
}
鑑於這種HTML文檔:
<html>
<head></head>
<body>
<!--<div> hello there </div>-->
<div>not a comment</div>
<!-- <h5>another comment</h5> -->
</body>
</html>
會導致這樣的輸出:
<html>
<head></head>
<body>
<div>hello there</div>
<div>not a comment</div>
<h5>another comment</h5>
</body>
</html>
相關問題
- 1. 如何使用Jsoup
- 2. 如何使用Jsoup
- 3. 如何使用jsoup
- 4. 如何使用jsoup
- 5. 如何使用Jsoup
- 6. 如何使用Jsoup
- 7. 如何使用Jsoup
- 8. 如何使用jsoup
- 9. 如何使用jsoup
- 10. 如何使用jsoup整理
- 11. JSOUP - 如何使用JSOUP抓取「需要登錄」頁面
- 12. 如何使用jsoup解析JavaScript
- 13. 如何使用jsoup設置jsessionid cookie?
- 14. 如何從SOAP響應使用Jsoup
- 15. 如何使用jsoup替換標籤
- 16. 如何使用JSoup發佈文件?
- 17. 如何使用JSoup獲取特定值?
- 18. 如何獲得使用Jsoup爲Android
- 19. 如何使用jsoup提交表格
- 20. 如何使用JSOUP單擊按鈕?
- 21. 如何在Android中使用JSOUP?
- 22. 如何使用Jsoup進行JIRA登錄?
- 23. 如何在jsoup中使用OR操作?
- 24. 如何從Java中使用jSoup
- 25. 如何使用Jsoup遍歷HTML樹?
- 26. 如何使用JSoup構建NodeTraversor/NodeVisitor?
- 27. 如何使用Jsoup提取此數據?
- 28. 如何使用Jsoup獲取此文本?
- 29. 如何使用Jsoup管理cookie?
- 30. 如何在Eclipse中使用Jsoup
評論類,這是關鍵。 –