我正在使用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();
}
考虑到解决方法,为什么我会收到此错误?
答案 0 :(得分:4)
使用try / catch块
try{
Document doc = new DomSerializer(props, true).createDOM(tagNode);
}
catch(DOMException e){
//error handling here if you want
}
//we now hit more code
答案 1 :(得分:2)
如果您正在处理上面提到的文件列表,则只需要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();
}
}