我正在使用 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/