Reputation: 3221
When I parse the html file(stored in native) with jsoup. I have modified some elements in the html file,so I want to save the modified html, and replace the old one?
Do any body know which method in jsoup can do the job?
Thank you so much!!!
Upvotes: 3
Views: 7545
Reputation: 159754
You could write the contents of either
document.toString()
or
document.outerHtml()
to file, where document
is got from
Document document = Jsoup.connect("http://...").get();
// any document modifications...
like so:
BufferedWriter htmlWriter =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
htmlWriter.write(document.toString());
Upvotes: 9
Reputation: 165
the declared answer that has 6 votes is correct all except for one part, it needs 1 more line of code.
Either "htmlWriter.close();" OR "htmlWriter.flush();" or both if you want. At the end of his code segment because I had the same issue and I used his version but he was missing this part (seen from the first comment on the post: gist.github.com/4139609. So the finished code segment is:
BufferedWriter htmlWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
System.out.println("\n" + doc.outerHtml());
htmlWriter.write(doc.toString());
htmlWriter.flush();
htmlWriter.close();
Upvotes: 3
Reputation: 16541
Change your modified jSoup Element to HTML String:
http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#html%28%29
String html = document.html();
Write to file:
Writer writer = new PrintWriter("/file.html", html);
writer.write(html);
writer.close();
More info here: Add custom css to html code with jsoup
Upvotes: 3