简体   繁体   中英

what method in jsoup can return the modified html?

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!!!

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());

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM