Fernando Costa Fernando Costa - 1 year ago 176
Java Question

How to disable XMLUnit DTD validation?

I am trying to compare two XHTML documents using XMLUnit 2.2.0. However, it is taking too long. I guess the library is downloading DTD files from Internet.

How can I disable DTD validation? I am using the following test code:

public class Main {
public static void main(String args[]) {
Diff d = DiffBuilder.compare(
"<!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"
"<!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"
for (Difference dd: d.getDifferences()) {

Reading the XMLUnit Javadoc of
, I thought I could do this setting a document builder factory like this...

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

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(

It did not work. My code runs fast just when I remove the DOCTYPE definition from the XHTML snippets.

Answer Source

withDocumentBuilderFactory is exactly what you want to use but unfortunately ignoreWhitespace defeats it.

Under the covers DiffBuilder creates a WhitespaceStrippedSource which creates a DOM Document without using the DocumentBuilderFactory you've configured. This is a bug. Do you want to create an issue for this?

A workaround using XMLUnit 2.2.0 is to create the Documents yourself, something like

Document control = Convert.toDocument(Input.fromString(...), dbf);
Document test = ...
Diff d = DiffBuilder.compare(Input.fromDocument(control))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download