What method in jsoup can return the modified html?

When I parse the html file (stored in native) with jsoup . I changed some elements in the html file, so I want to save the changed html and replace the old one?
Can any body know which method in jsoup can do the job?
Thank you very much.

+3
source share
3 answers

You can write the contents either

document.toString() 

or

 document.outerHtml() 

to the file where document obtained from

 Document document = Jsoup.connect("http://...").get(); // any document modifications... 

So:

 BufferedWriter htmlWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); htmlWriter.write(document.toString()); 
+9
source

Change the modified jSoup element to an HTML string:

http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#html%28%29

 String html = document.html(); 

Write to the file:

 Writer writer = new PrintWriter("/file.html", html); writer.write(html); writer.close(); 

Read more here: Add custom CSS code to html code using jsoup

+2
source

the announced answer, which has 6 votes, is correct, with the exception of one part, it requires 1 more line of code.

Or "htmlWriter.close ();" OR "htmlWriter.flush ();" or both if you want. At the end of his code segment, because I had the same problem, and I used his version, but he lacked this part (seen from the first comment on the post: gist.github.com/4139609. So, the finished code segment :

 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(); 
+2
source

Source: https://habr.com/ru/post/925652/


All Articles