我正在使用xml解析器从文件夹中读取HTML文件,该文件将干净代码存储在tagNode中
try {
Document doc = new DomSerializer(props, true).createDOM(tagNode);
} catch (Exception ex) {
ex.printStackTrace();
}
但是其中一个文件给我一个错误:
org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
发现异常后如何继续运行程序?
解决方案#1
try
{
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
FileWriter fstream = new FileWriter("dataset.txt");
BufferedWriter br= new BufferedWriter(fstream);
for (int i = 0; i < listOfFiles.length; i++) {
{
try {
Document doc = new DomSerializer(props, true).createDOM(tagNode);
} catch (Exception ex) {
ex.printStackTrace();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
有一种解决方法,为什么我会收到此错误?
最佳答案
如果要像上面提到的那样处理文件列表,则只需要for循环内的try-catch
块:
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
FileWriter fstream = new FileWriter("dataset.txt");
BufferedWriter br= new BufferedWriter(fstream);
for (int i = 0; i < listOfFiles.length; i++)
{
try
{
Document doc = new DomSerializer(props, true).createDOM(tagNode);
}
catch (DOMException de)
{
de.printStackTrace();
}
}