I have few HTMLS where I want to find particular divs and update them with new Lines of Code.
Currently, I am using Jsoup as HTML parser in my JAVA backend but somehow it is not updating required content INSTEAD, removing Scripts and other sections from existing HTMLs.
Here is the code I am using.
File file = new File("C:/2015/05 May/sample.html");
Document doc = Jsoup.parse(file, "UTF-8");
// clear the <head> element and add a new <script> element.
doc.head().appendElement("script")
.attr("type", "text/javascript")
.attr("src", "newScriptToAddInHeaderSection.js");
for( Element element : doc.select("div.sampleDiv") )
{
element.remove();
System.out.println("REMOVED..");
}
//write the changed HTML to the same file.
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(doc.html());
On execution of this code. It will add new JS file in Header Section. Then, It is removing "sampleDiv" but also other scripts and divs from my HTML. I only need to update this "sampleDiv" with new Content.
Please help!!
Aucun commentaire:
Enregistrer un commentaire