如何禁用 XMLUnit DTD 验证?
How to disable XMLUnit DTD validation?
我正在尝试使用 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());
}
}
}
阅读 DiffBuilder.withDocumentBuilderFactory()
的 XMLUnit Javadoc,我想我可以像这样设置文档生成器工厂...
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 定义时,我的代码运行速度很快。
withDocumentBuilderFactory
正是您想要使用的,但不幸的是 ignoreWhitespace
打败了它。
在幕后 DiffBuilder
创建一个 WhitespaceStrippedSource
,它创建一个 DOM Document
,而不使用您配置的 DocumentBuilderFactory
。这是一个错误。你想为此创建一个问题吗?
使用 XMLUnit 2.2.0 的解决方法是自己创建 Document
,例如
Document control = Convert.toDocument(Input.fromString(...).build(), dbf);
Document test = ...
Diff d = DiffBuilder.compare(Input.fromDocument(control))
.withTest(Input.fromDocument(test))
.ignoreWhitespace().build();
编辑:该错误已在 XMLUnit 2.2.1 中修复,问题代码现在无需任何更改即可正常运行。
我正在尝试使用 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());
}
}
}
阅读 DiffBuilder.withDocumentBuilderFactory()
的 XMLUnit Javadoc,我想我可以像这样设置文档生成器工厂...
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 定义时,我的代码运行速度很快。
withDocumentBuilderFactory
正是您想要使用的,但不幸的是 ignoreWhitespace
打败了它。
在幕后 DiffBuilder
创建一个 WhitespaceStrippedSource
,它创建一个 DOM Document
,而不使用您配置的 DocumentBuilderFactory
。这是一个错误。你想为此创建一个问题吗?
使用 XMLUnit 2.2.0 的解决方法是自己创建 Document
,例如
Document control = Convert.toDocument(Input.fromString(...).build(), dbf);
Document test = ...
Diff d = DiffBuilder.compare(Input.fromDocument(control))
.withTest(Input.fromDocument(test))
.ignoreWhitespace().build();
编辑:该错误已在 XMLUnit 2.2.1 中修复,问题代码现在无需任何更改即可正常运行。