How to use XMLUnit2 to compare xmls by XPATH?

1.6k Views Asked by At

I am trying to compare two xml only partially using XMLUnit2. I tried below Xpath to compare element int alone. But my test is failed because, it is checking boolean tag as well.

How do I make this test pass?

@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());


    }

Edit: With NodeFilter, I removed the unwanted nodes from comparison. But is there a way to give Xpath, and compare only the nodes evaluated by 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());


    }
1

There are 1 best solutions below

1
Manoj On

I don't know whether this is the right approach. But posted here anyway to get comments from you. In this way, I can only compare the XPath evaluated XML node list.

@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;
    }