Bạn có thể sử dụng API JTidy để thực hiện việc này. Sử dụng jtidy-r938.jar
Bạn có thể sử dụng các phương pháp sau đây để có được xhtml từ html
public static String getXHTMLFromHTML(String inputFile,
String outputFile) throws Exception {
File file = new File(inputFile);
FileOutputStream fos = null;
InputStream is = null;
try {
fos = new FileOutputStream(outputFile);
is = new FileInputStream(file);
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.parse(is, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
fos = null;
}
fos = null;
}
if(is != null){
try {
is.close();
} catch (IOException e) {
is = null;
}
is = null;
}
}
return outputFile;
}
Nguồn
2016-06-29 14:32:26
Strange, nó hoạt động tốt đối với tôi. Tôi đã thử nghiệm nó bằng cách sử dụng phiên bản '1.7.2'. – Pshemo
Không làm việc cho tôi, tôi đang sử dụng '1.8.1' – Henry