java java中html到xhtml的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5936403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
html to xhtml conversion in java
提问by yagnya
how can we convert html to well formed xhtml by using Http class api,if possible please give a demonstration code....thanks
我们如何通过使用 Http 类 api 将 html 转换为格式良好的 xhtml,如果可能,请给出演示代码....谢谢
回答by Vitor Pelizza
I just did it using Jsoup, if it works for you:
我只是用 Jsoup 做的,如果它适合你:
private String htmlToXhtml(final String html) {
final Document document = Jsoup.parse(html);
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
return document.html();
}
Some useful content where my solution came from:
我的解决方案来自的一些有用内容:
回答by mglauche
Have a look at J-Tidy: http://jtidy.sourceforge.net/It usually does a quite good job cleaning up messy html and converting it to xhtml.
看看 J-Tidy:http: //jtidy.sourceforge.net/它通常可以很好地清理凌乱的 html 并将其转换为 xhtml。
回答by Tanmay kumar shaw
You can use the following method to get xhtml from html
您可以使用以下方法从 html 中获取 xhtml
public static String getXHTMLFromHTML(String inputFile,
String outputFile) throws Exception {
File file = new File(inputFile);
FileOutputStream fos = null;
InputStream is = null;
try {
fos = new FileOutputStream(outputFile);
is = new FileInputStream(file);
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.parse(is, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
fos = null;
}
fos = null;
}
if(is != null){
try {
is.close();
} catch (IOException e) {
is = null;
}
is = null;
}
}
return outputFile;
}