我有 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 忽略命名空间验证?