首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在编译xml XMLUnit时忽略某些元素。

在编译xml XMLUnit时忽略某些元素。
EN

Stack Overflow用户
提问于 2016-09-26 18:21:16
回答 1查看 902关注 0票数 1

我想在一个Junit测试中比较两个XML。我使用XMLUnit来比较xml.Can,请告诉我有没有什么简单的方法可以忽略correlation-id在xmls中的比较。

XML1:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<response>
<bih-metadata>
<result>Error</result>
<correlation-id>ID:925977d0-83cd-11e6-b94d-c135e6c73218</correlation-id>
<error-message>SAXParseException: The entity name must immediately follow the '&amp;' in the entity reference.</error-message>
</bih-metadata>
</response>

XML2:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<response>
<bih-metadata>
<result>Error</result>
<correlation-id>ID:134345d0-83cd-11e6-b94d-c135e6c73218</correlation-id>
<error-message>SAXParseException: The entity name must immediately follow the '&amp;' in the entity reference.</error-message>
</bih-metadata>
</response>
EN

回答 1

Stack Overflow用户

发布于 2016-09-26 21:50:16

这是在XMLUnit 2.x中添加NodeFilter的目的:

代码语言:javascript
复制
import org.w3c.dom.Element;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.util.Nodes;
import org.xmlunit.diff.*;

public class Test {

    public static void main(String[] args) {
        Diff d = DiffBuilder.compare("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                                     "<response>\n" +
                                     "<bih-metadata>\n" +
                                     "<result>Error</result>\n" +
                                     "<correlation-id>ID:925977d0-83cd-11e6-b94d-c135e6c73218</correlation-id>\n" +
                                     "<error-message>SAXParseException: The entity name must immediately follow the '&amp;' in the entity reference.</error-message>\n" +
                                     "</bih-metadata>\n" +
                                     "</response>")
            .withTest("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                      "<response>\n" +
                      "<bih-metadata>\n" +
                      "<result>Error</result>\n" +
                      "<correlation-id>ID:134345d0-83cd-11e6-b94d-c135e6c73218</correlation-id>\n" +
                      "<error-message>SAXParseException: The entity name must immediately follow the '&amp;' in the entity reference.</error-message>\n" +
                      "</bih-metadata>\n" +
                      "</response>")
            .withNodeFilter(n -> !(n instanceof Element && "correlation-id".equals(Nodes.getQName(n).getLocalPart())))
            .build();
        System.err.println("Different? " + d.hasDifferences());
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39700281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档