Java:从html文本到xhtml文本的Jtidy转换

标签 java html xhtml jtidy

我正在使用 JTidy,我想给它一个字符串作为输入而不是一个文件。那可能吗? 我该怎么做?

这是我的代码:

    FileInputStream fis =null;  
    String htmlFileName = "report.html";  

   //from html to xhtml
   try   
    {  
        fis = new FileInputStream(htmlFileName);  
    }  
    catch (java.io.FileNotFoundException e)   
    {  
        System.out.println("File not found: " + htmlFileName);  
    }  
        Tidy tidy = new Tidy(); 
        tidy.setShowWarnings(false);
        tidy.setXmlTags(false);
        tidy.setInputEncoding("UTF-8");
        tidy.setOutputEncoding("UTF-8");
        tidy.setXHTML(true);// 
        tidy.setMakeClean(true);
        Document xmlDoc = tidy.parseDOM(fis, null);  
    try  
    {  
        tidy.pprint(xmlDoc,new FileOutputStream("report.xhtml"));  
    }  

最佳答案

FileInputStream 替换为从 String 读取的流,例如

try   
{
    fis = new ByteArrayInputStream(string.getBytes());
}  catch (java.io.IOException e) {  
    System.out.println("Error reading string");
    return;
}  

关于Java:从html文本到xhtml文本的Jtidy转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15337357/

相关文章:

javascript - XHTML 或 SVG 中的 Ecmascript

java - 将文本粘贴到 JTextComponent 后获取操作 (Java)

html - 覆盖div的溢出:hidden

html - 在窗口 addEventListener popstate 中调用函数,不起作用

html - 如何正确转义 HTML 属性中的引号?

html - 当页面作为 HTML 4.01 提供时,是否需要 <html xmlns= ...> 中的 "xmlns"?

java - 在 Play 框架和 Django 之间共享 session

Java 正则表达式 : Replace all characters with `+` except instances of a given string

Java 密码检查

php - 想要为 MailChimp 提供动态内容?