HTML文件转换成XML文件
HTML文件成XML文件
char[] buffer = new char[10240]; // 文件缓冲区
int len = 0; // 使用字符读取方式,循环读取源文件内容
while( (len = isr.read(buffer)) !=-1 ) // 转换后写入目标文件中
{
osw.write( buffer, 0, len);
}
osw.close(); // 转换完成
isr.close();
out.close();
in.close();
if( log.isLoggable( Level.INFO)){
log.info("HTML 文档转 UTF-8 编码完成!");
}
//设置tidy
Tidy tidy = new Tidy();
// Set file for error messages
tidy.setErrout(new PrintWriter(new FileWriter(errOutFileName), true));
// Tell Tidy to convert HTML to XML
tidy.setXmlOut(true);
tidy.setInputEncoding("UTF-8");
FileInputStream in0 = new FileInputStream( tmpNewFile );
FileOutputStream out0 = new FileOutputStream(outFileName);
//Convert files
tidy.parse(in0, out0);
//Clean up
in.close();
out.close();
tmpNewFile.delete(); // 删除临时文件
} catch (IOException e) {
System.out.println(this.toString() + e.toString());
}
}
public static void main(String[] args) {
/*
* Parameters are:
* URL of HTML file
* Filename of output file
* Filename of error file
*/
String u="http://www.baidu.com/";
String o="index.xml";
String e="error.xml";
xml t = new xml(u, o, e);
t.convert();
System.out.println("OK!");
}
}