what method in jsoup can return the modified html?

随声附和 提交于 2019-12-12 09:49:46

问题


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


回答1:


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



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/13541460/what-method-in-jsoup-can-return-the-modified-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!