I want to remove from the html document from the embed tag the style attribute:

 <div> <embed class="style_5" id="__bookmark_3" onresize="document.getElementById('__bookmark_3').reload()" type="image/svg+xml" src="image/custom10.svg" alt="" style=" width: 212pt; height: 285.75pt;display: block;"></embed> </div> 

For this, I use the jsoup library and the following code:

 File generatedReport = new File(outputReportPath); if (generatedReport.exists()) { logger.info("generatedReport exists"); } else { logger.info("generatedReport doesn't exist"); } Document doc = Jsoup.parse(generatedReport, "UTF-8"); Element embed = doc.select("embed").first(); String styleAttr = embed.attr("style"); logger.info("styleAttr: " + styleAttr); embed.removeAttr("style"); 

And after running in the logs such a conclusion:

... | INFO | generatedReport exists

... | INFO | styleAttr: width: 212pt; height: 285.75pt; display: block;

Those. everything seems to work out well, except that the tag is not removed from the html file. What am I doing wrong? Or does this method only remove the attribute from the object model in the program's memory, and do you need to overwrite the file in order to actually remove the attribute from the tag of the hmtl file?

    1 answer 1

    Yes, as I expected, this method only removes the attribute from the object model in the program's memory, and you need to overwrite the file in order to actually remove the attribute from the tag of the hmtl file. After adding this code:

     PrintWriter writer = new PrintWriter(generatedReport,"UTF-8"); writer.write(doc.html()); writer.flush(); writer.close(); 

    The html file was changed and the embed tag looked like

     <embed class="style_5" id="__bookmark_3" onresize="document.getElementById('__bookmark_3').reload()" type="image/svg+xml" src="image/custom10.svg" alt=""> 

    as required.