首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:从html文本到xhtml文本的Jtidy转换

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

Stack Overflow用户
提问于 2013-03-11 19:23:52
回答 1查看 2K关注 0票数 2

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

这是我的代码:

代码语言:javascript
复制
    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"));  
    }  
EN

回答 1

Stack Overflow用户

发布于 2013-03-11 19:27:22

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

代码语言:javascript
复制
try   
{
    fis = new ByteArrayInputStream(string.getBytes());
}  catch (java.io.IOException e) {  
    System.out.println("Error reading string");
    return;
}  
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15337357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档