techiepark
techiepark

Reputation: 343

How to remove/replace following inline css style from html code in Java

I have a html page in which following master reset css is included. I'll be getting html code as a string in java, from which i have to remove/replace/comment following css code using java. I have to exclude other inline css styles while removing/replacing below css. I tried using StringUtils class, but its not working. How i can do this in 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>

Upvotes: 3

Views: 4085

Answers (1)

dogbane
dogbane

Reputation: 274660

I'd recommend using an HTML parsing library such as JSoup to do this.

With JSoup, you can select certain elements (based on their tagname, id etc) using a selector. For example, to remove all the style elements:

Document doc = Jsoup.parse(html);
Elements els = doc.select("style");
for(Element e: els){
    e.remove();
}

Upvotes: 5

Related Questions