Created
November 2, 2012 06:49
-
-
Save relax-more/3999122 to your computer and use it in GitHub Desktop.
[Java] JsoupでコメントアウトのNodeを取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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