html to xhtml conversion in java

前端 未结 3 416
别跟我提以往
别跟我提以往 2020-12-11 20:36

how can we convert html to well formed xhtml by using Http class api,if possible please give a demonstration code....thanks

相关标签:
3条回答
  • 2020-12-11 20:52

    I just did it using Jsoup, if it works for you:

    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:

    • Is it possible to convert HTML into XHTML with Jsoup 1.8.1?
    • http://developers.itextpdf.com/question/how-do-html-xml-conversion-generate-closed-tags
    0 讨论(0)
  • 2020-12-11 20:59

    You can use the following method to get xhtml from html

    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;
        }
    
    0 讨论(0)
  • 2020-12-11 21:11

    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.

    0 讨论(0)
提交回复
热议问题