我正在尝试使用XMLUnit 2.2.0 比较两个 XHTML 文档。但是,它需要的时间太长。我猜图书馆正在从 Internet 下载 DTD 文件。
如何禁用 DTD 验证?我正在使用以下测试代码:
public class Main {
public static void main(String args[]) {
Diff d = DiffBuilder.compare(
Input.fromString(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \n"
+" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
+"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+" <head></head>\n"
+" <body>some content 1</body>\n"
+"</html>")).withTest(
Input.fromString(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \n"
+" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
+"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+" <head></head>\n"
+" <body>some content 2</body>\n"
+"</html>")).ignoreWhitespace().build();
if(d.hasDifferences())
for (Difference dd: d.getDifferences()) {
System.out.println(dd.toString());
}
}
}
阅读XMLUnit Javadoc,DiffBuilder.withDocumentBuilderFactory()
我想我可以像这样设置一个文档构建器工厂...
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setFeature("http://xml.org/sax/features/validation", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Diff d = DiffBuilder.compare(Input.fromString(...)).withTest(
Input.fromString(...)).withDocumentBuilderFactory(dbf)
.ignoreWhitespace().build();
那没起效。就在我从 XHTML 片段中删除 DOCTYPE 定义时,我的代码运行得很快。