比较XML文件时的XMLUNIT命名空间异常

时间:2016-07-12 05:09:43

标签: xml xml-namespaces xmldiff xmlunit-2

我有2个XML文件,在使用XMLUNIT进行比较时,我得到以下异常。

Exception in thread "main" org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
    at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.checkDOMNSErr(CoreDocumentImpl.java:2530)
    at com.sun.org.apache.xerces.internal.dom.AttrNSImpl.setName(AttrNSImpl.java:117)
    at com.sun.org.apache.xerces.internal.dom.AttrNSImpl.<init>(AttrNSImpl.java:78)
    at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createAttributeNS(CoreDocumentImpl.java:2142)
    at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode(CoreDocumentImpl.java:1596)
    at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode(CoreDocumentImpl.java:1560)
    at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.cloneNode(CoreDocumentImpl.java:362)
    at com.sun.org.apache.xerces.internal.dom.DocumentImpl.cloneNode(DocumentImpl.java:159)
    at org.custommonkey.xmlunit.XMLUnit.stripWhiteSpaceWithoutXSLT(XMLUnit.java:522)
    at org.custommonkey.xmlunit.XMLUnit.getWhitespaceStrippedDocument(XMLUnit.java:506)
    at org.custommonkey.xmlunit.Diff.getWhitespaceManipulatedDocument(Diff.java:182)
    at org.custommonkey.xmlunit.Diff.getManipulatedDocument(Diff.java:203)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:155)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:168)
    at org.custommonkey.xmlunit.DetailedDiff.<init>(DetailedDiff.java:60)

我使用的是以下代码,但无法正常使用。

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);

    try {
        factory.setFeature("http://xml.org/sax/features/namespaces", false);
        factory.setFeature("http://xml.org/sax/features/validation", false);
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    XMLUnit.setTestDocumentBuilderFactory(factory);
    XMLUnit.setControlDocumentBuilderFactory(factory);
    XMLUnit.setIgnoreWhitespace(Boolean.TRUE);
    XMLUnit.setIgnoreComments(true);
    XMLUnit.setIgnoreAttributeOrder(true);

如何使XMLUNIT忽略名称空间验证?

1 个答案:

答案 0 :(得分:0)

删除以下2行使我的代码按预期工作。

factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);