我有一个 html 页面,其中包含以下主重置 css。我将在 java 中将 html 代码作为字符串获取,我必须从中删除/替换/注释以下使用 java 的 css 代码。在删除/替换 css 下方时,我必须排除其他内联 css 样式。我尝试使用 StringUtils 类,但它不起作用。我怎么能在java中做到这一点?
<style type="text/css">
@charset "utf-8";
/* CSS Document */
/* Ver 1.0 Author*/
/* master reset */
a,abbr,acronym,address,applet,b,big,blockquote,body,button,caption,center,cite,code,dd,del,dfn,
dir,div,dl,dt,em,embed,fieldset,font,form,frame,h1,h2,h3,h4,h5,h6,hr,html,i,iframe,img,input,
ins,kbd,label,legend,li,menu,object,ol,option,p,pre,q,s,samp,select,small,span,strike,strong,
sub,sup,table,tbody,td,textarea,tfoot,th,thead,tr,tt,u,ul,var
{background:transparent;border:0;font-family:inherit;font-size:100%;font-style:inherit;
font-weight:inherit;margin:0;outline:0;padding:0;vertical-align:baseline;}
html {font-size:1em;overflow-y:scroll;}
body {background:white;color:black;line-height:1;}
a,ins {text-decoration:none;}
blockquote,q{quotes:none;quotes:"" "";}
blockquote:before,blockquote:after,q:before,q:after {content:"";content:none;}
caption,center,td,th {text-align:left;}
del {text-decoration:line-through;}
dir,menu,ol,ul {list-style:none;}
table {border-collapse:collapse;border-spacing:0;}
textarea {overflow-y:auto;}
</style>
Run Code Online (Sandbox Code Playgroud)
我建议使用 HTML 解析库(例如JSoup)来执行此操作。
随着JSoup,您可以选择某些元素(根据自己的标记名,身份证等)使用选择。例如,要删除所有style元素:
Document doc = Jsoup.parse(html);
Elements els = doc.select("style");
for(Element e: els){
e.remove();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3337 次 |
| 最近记录: |