2017-09-25 88 views
0

我想比較兩個XML只部分使用XMLUnit2。我在Xpath下面嘗試單獨比較元素int。但是我的測試失敗了,因爲它正在檢查boolean標記。如何使用XMLUnit2通過XPATH比較xmls?

我該如何通過此測試?

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


    } 
+0

我不知道我明白你想要比較什麼。你想比較所有'struct'還是僅僅'int's? 'NodeMatcher'用於決定一組兄弟節點中的哪些節點進行比較,它根本不會影響哪些節點有資格進行比較。你會使用'NodeFilter'來去掉那些你不想要的東西。 –

+0

我想比較兩個xml中的'int'標記。我不在乎布爾標籤是否匹配。 – Manoj

+0

由於我編輯過,我可以使用Nodefilter刪除不需要的節點。有沒有辦法使用Xpath和比較? – Manoj

回答

0

我不知道這是否正確。但無論如何張貼在這裏,以獲得您的意見。這樣,我只能比較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; 
    } 
+0

您可以通過使用XMLUnit的[XPath抽象](https://github.com/xmlunit/user-guide/wiki/XPath-Support)保存幾行,但是,這可能是現在最好的方法。在將來的版本中,'Input'構建器可能會被修改爲接受一個'NodeList'作爲源,甚至是一個'Source'和一個XPath表達式來執行相同的任務。 –