javaxmlentityentitiesxmlunit

Using XMLUnit(Java) to compare XML strings containing entities


JDK 1.7.0 XMLUnit 1.3

When comparing this control XML string:

            String controlXml = "" +
                "<client>" +
                "   <name>Hello&nbsp;World</name>" +
                "</client>";

With this test XML string:

            String testXml = "" + 
                "<client>" +
                "   <name>Hello&nbsp;World</name>" +
                "</client>";

XMLUNIT returns false and I was expecting it to return true (no differences).

Here is my usage:

XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreWhitespace(true);
Diff diff = new Diff(controlXml,testXml);
boolean result = diff.similar(); //result is false

I also get the following error in the console window:

[Fatal Error] :1:103: The entity "nbsp" was referenced, but not declared.

I'm not sure what to do here.

I looked into it and I got some info about EntityResolver using Google but it's all very confusing.

I need the entity to be treated as plain text. Logically, as plain text, they are equal.

I tried toggling with the following options:

        XMLUnit.setExpandEntityReferences(false);  //tried true, false
        XMLUnit.setIgnoreDiffBetweenTextAndCDATA(false);  //tried true, false

Nothing worked. Please help, I'm totally lost. Thanks!


Solution

  • It appears that there's a difference between character entities ( ) and numerical entities ( ).

    XMLUnit has no problems with numerical entities as opposed to character entities.

    I'm thinking that a simple character-entity-to-numerical-entity procedure should be sufficient to resolve my problem.