我使用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;
}