在Java中取消注释HTML代码

时间:2014-03-31 13:55:10

标签: java html html-parsing

我需要在样式元素中取消注释CSS代码(我需要在Java中执行此操作)。 请考虑以下HTML代码:

<html>
<head>
<style type="text/css">
<!--
   .big {
      font-size: 30px;
   }
-->
</style>
</head>
<body></body>
</html>

这是期望的结果:

<html>
<head>
<style type="text/css">
   .big {
      font-size: 30px;
   }
</style>
</head>
<body></body>
</html>

我通常使用Jericho进行HTML解析。

更新。解决:

String newHtmlString = htmlString.replaceAll("<style><!--", "<style>").replaceAll("--></style>", "</style>");

1 个答案:

答案 0 :(得分:3)

如果您的文件中唯一的评论是CSS评论,您可能会考虑以下内容:

    String html = ...; //HTML in String
    html.replaceAll("<!--", "");
    html.replaceAll("-->", "");