Skip to content

Instantly share code, notes, and snippets.

@relax-more
Created November 2, 2012 06:49
Show Gist options
  • Save relax-more/3999122 to your computer and use it in GitHub Desktop.
Save relax-more/3999122 to your computer and use it in GitHub Desktop.
[Java] JsoupでコメントアウトのNodeを取得する
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
public class RemoveComments {
public static void main(String... args) {
String h = "<html><head></head><body>" +
"<div><!-- foo --><p>bar<!-- baz --></div><!--qux--></body></html>";
Document doc = Jsoup.parse(h);
removeComments(doc);
System.out.println(doc.html);
}
private static void removeComments(Node node) {
for (int i = 0; i < node.childNodes().size()) {
Node child = node.childNode(i);
if (child.nodeName().equals("#comment"))
child.remove();
else {
removeComments(child);
i++;
}
}
}
}
/**
* 【出典】
* How to search for comments (“<!— -->”) using Jsoup?
* http://stackoverflow.com/questions/7541843/how-to-search-for-comments-using-jsoup
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment