【问题标题】:How to use XMLUnit2 to compare xmls by XPATH?如何使用 XMLUnit2 通过 XPATH 比较 xmls?
【发布时间】:2017-09-25 12:19:07
【问题描述】:

我正在尝试仅部分使用 XMLUnit2 比较两个 xml。我尝试在 Xpath 下单独比较元素 int。但是我的测试失败了,因为它也在检查boolean 标签。

如何让这个测试通过?

@Test
    public void diff_through_xPath(){
        String myControlXML = "<struct><boolean>false</boolean><int>3</int></struct>";
        String myTestXML = "<struct><boolean>true</boolean><int>3</int></struct>";

        ElementSelector childSelector = selectorForElementNamed("int", byXPath("//int", byName));

        Diff myDiffSimilar = DiffBuilder.compare(myControlXML).withTest(myTestXML)
                .withNodeMatcher(new DefaultNodeMatcher(childSelector, byName))
                .checkForSimilar()
                .ignoreWhitespace()
                .build();

        assertFalse("XML similar " + myDiffSimilar.toString(),
                myDiffSimilar.hasDifferences());


    }

编辑: 使用 NodeFilter,我从比较中删除了不需要的节点。但是有没有办法给出 Xpath,并且只比较由 XPath 评估的节点。

@Test
    public void diff_through_xPath() {
        String myControlXML = "<struct><boolean>false</boolean><int>3</int></struct>";
        String myTestXML = "<struct><boolean>true</boolean><int>3</int></struct>";

        Diff myDiffSimilar = DiffBuilder.compare(myControlXML).withTest(myTestXML)
                .withNodeFilter(new Predicate<Node>() {
                    public boolean test(Node node) {
                        return !node.getNodeName().equals("boolean"); //ignores all child nodes from of 'a'.
                    }
                })
                //.checkForSimilar()
                .ignoreWhitespace()
                .build();

        assertFalse("XML similar " + myDiffSimilar.toString(),
                myDiffSimilar.hasDifferences());


    }

【问题讨论】:

  • 我不确定我是否理解您想要比较的内容。你想比较structs 还是只比较ints? NodeMatcher 用于决定一组兄弟节点中的哪些节点可以相互比较,它对哪些节点有资格进行比较根本没有任何影响。你可以使用NodeFilter 去掉那些你不想要的。
  • 我只想比较两个 xml 中的 'int' 标签。我不在乎布尔标记是否匹配。
  • 正如我所编辑的,我可以使用 Nodefilter 删除不需要的节点。有没有办法使用 Xpath 并进行比较?

标签: java junit xmlunit xmlunit-2


【解决方案1】:

我不知道这是否是正确的方法。但无论如何张贴在这里从你那里得到cmets。这样,我只能比较 XPath 评估的 XML 节点列表。

@Test
    public void testXpath() throws Exception {

        File controlFile = new File("src/test/resources/myControlXML.xml");
        File testFile = new File("src/test/resources/myTest.xml");


        Diff myDiff = DiffBuilder.compare(Input.fromDocument(getDocument("src/test/resources/myControlXML.xml", "/struct/int")))
                .withTest(Input.fromDocument(getDocument("src/test/resources/myTest.xml", "/struct/int")))
                .checkForSimilar().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
                .ignoreWhitespace()
                .build();

        assertFalse("XML similar " + myDiff.toString(),
                myDiff.hasDifferences());
    }

    public Document getDocument(String fileName, String xpathExpression) throws Exception {
        File controlFile = new File(fileName);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;

        dBuilder = dbFactory.newDocumentBuilder();

        Document controlDoc = dBuilder.parse(controlFile);
        controlDoc.getDocumentElement().normalize();

        XPath xPath = XPathFactory.newInstance().newXPath();

        NodeList nodes = (NodeList) xPath.compile(xpathExpression).evaluate(
                controlDoc, XPathConstants.NODESET);

        Document newXmlDocument = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder().newDocument();
        Element root = newXmlDocument.createElement("root");
        newXmlDocument.appendChild(root);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            Node copyNode = newXmlDocument.importNode(node, true);
            root.appendChild(copyNode);
        }

        return newXmlDocument;
    }

【讨论】:

  • 您可以使用 XMLUnit 的 XPath abstractions 节省几行代码,但是是的,这可能是目前最好的方法。在未来的版本中,Input 构建器可能会被修改为接受 NodeList 作为源,甚至接受 Source 和 XPath 表达式以在后台执行相同的任务。
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
相关资源
最近更新 更多