Created
July 26, 2010 22:59
-
-
Save jhy/491407 to your computer and use it in GitHub Desktop.
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
// example solution to remove comments from HTML. | |
// re: http://groups.google.com/group/jsoup/browse_thread/thread/419b5ac4be88b086 | |
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 = "<div><!-- no --><p>Hello<!-- gone --></div>"; | |
Document doc = Jsoup.parse(h); | |
removeComments(doc); | |
print(doc.html()); | |
} | |
private static void removeComments(Node node) { | |
// as we are removing child nodes while iterating, we cannot use a normal foreach over children, | |
// or will get a concurrent list modification error. | |
int i = 0; | |
while (i < node.childNodes().size()) { | |
Node child = node.childNode(i); | |
if (child.nodeName().equals("#comment")) | |
child.remove(); | |
else { | |
removeComments(child); | |
i++; | |
} | |
} | |
} | |
private static void print(String msg) { | |
System.out.println(msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work. You saved my lots of time..
Thanks dear.