html到java中的xhtml转换
如何通过使用Http类api将html转换为格式良好的xhtml,如果可能请提供演示代码….谢谢
我只是使用Jsoup,如果它适合你:
private String htmlToXhtml(final String html) { final Document document = Jsoup.parse(html); document.outputSettings().syntax(Document.OutputSettings.Syntax.xml); return document.html(); }
我的解决方案来自的一些有用的内容:
- 是否可以使用Jsoup 1.8.1将HTML转换为XHTML?
- http://developers.itextpdf.com/question/how-do-html-xml-conversion-generate-closed-tags
看看J-Tidy: http : //jtidy.sourceforge.net/它通常可以很好地清理凌乱的HTML并将其转换为xhtml。
您可以使用以下方法从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; }