How To Compare 2 Xml Files Inwards Coffee - Xmlunit Example

The XMLUnit library tin endure used to compare 2 XML files inward Java. Similar to JUnit, XMLUnit tin also endure used to bear witness XML files for comparing past times extending the XMLTestcase class. It is a rich library together with provides a detailed comparing of XML files. Btw, comparing XML is completely unlike than comparing String inward Java or comparing object using equals(), every bit 2 XML which contains unlike comment together with whitespace tin endure equals, which is non truthful for String or grapheme comparison. Also spell comparing XML files, it's rattling of import to know just which content or business office is unlike together with XMLUnit non exclusively shows the content which is unlike but also XPath of elements which is getting compared.

The pump together with someone of XMLUnit are the DifferenceEngine flat but nosotros won't piece of job it directly. Instead, you lot volition piece of job Diff together with DetailedDiff the 2 of import classes for XML comparison. They furnish comparing engine for comparing XML. The XMLUnit library non exclusively compares consummate XML document but also tin perform a lot of useful activities related to XPath e.g. it tin banking company stand upwards for if an XPATH exists or non exists. It tin fifty-fifty banking company stand upwards for if XPath contains expected value or not.

The XMLUnit library tin also furnish validation support. So, if you lot desire to banking company stand upwards for if an XML file confirms to DTD or not, you lot tin exercise that using XMLUnit's Validator class. The default validation is using DTD but if you lot desire to confirm against XSD schema, you lot tin exercise that past times using choice Validator.useXMLSChema flag.




Java computer program to compare 2 XML documents inward Java.

It internally uses JAXP for XSLT transformation together with XPath evaluation. It at to the lowest degree needs JAXP 1.2 which is included inward Java 1.5 but if you lot desire to piece of job to a greater extent than advanced XPath engine based upon JAXP 1.3 together with then you lot necessitate to include Java 1.5 or higher version inward the classpath. In this Java together with XML tutorial, you lot volition larn how to compare 2 XML documents inward Java past times next a elementary illustration using XMLUnit.Overall it's a nice, handy utility library for testing together with comparing XML files inward Java applications.

Here is consummate Java computer program to compare 2 XML documents using XMLUnit library. In this example, nosotros receive got 2 XML files source.xml together with target.xml, afterwards is created past times copying the one-time file together with I receive got made 1 change, inward a telephone expose to compare these 2 XML files. In social club to compare together with exhibit differences betwixt XML documents, I receive got created 2 static methods compareXML() together with printDifferences().

The code to read XML file is rattling similar to the code of reading text files inward Java, nosotros are reading XML files every bit InputStream together with passing it to compareXML() every bit Reader object. Real XML comparing begins inward this method alongside Diff together with DetailedDiff class.

The DetailedDiff render all differences every bit List, if at that spot is no departure than the size of this List would endure nada together with nosotros tin say 2 XML files are identical inward data. printDifference() method takes this List of Differences together with prints it on Console. If you lot facial expression departure provided past times XMLUnit, you lot volition detect that it shows both what is the departure together with where is that departure happen past times showing consummate XPATH.



Java Program to compare XML files
package test;  import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.List; import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.Difference; import org.xml.sax.SAXException;  /**   *   * Java computer program to compare 2 XML files using XMLUnit illustration    * @author Javin Paul   */ public class XMLComparator {         public static void main(String args[]) throws FileNotFoundException,                                                    SAXException, IOException {               // reading 2 xml file to compare inward Java program         FileInputStream fis1 = new FileInputStream("C:/test/source.xml");         FileInputStream fis2 = new FileInputStream("C:/test/target.xml");               // using BufferedReader for improved performance         BufferedReader  origin = new BufferedReader(new InputStreamReader(fis1));         BufferedReader  target = new BufferedReader(new InputStreamReader(fis2));               //configuring XMLUnit to ignore white spaces         XMLUnit.setIgnoreWhitespace(true);               //comparing 2 XML using XMLUnit inward Java         List differences = compareXML(source, target);               //showing differences flora inward 2 xml files         printDifferences(differences);         }           public static List compareXML(Reader source, Reader target) throws                   SAXException, IOException{               //creating Diff instance to compare 2 XML files         Diff xmlDiff = new Diff(source, target);               //for getting detailed differences betwixt 2 xml files         DetailedDiff detailXmlDiff = new DetailedDiff(xmlDiff);               return detailXmlDiff.getAllDifferences();     }       public static void printDifferences(List differences){         int totalDifferences = differences.size();         System.out.println("===============================");         System.out.println("Total differences : " + totalDifferences);         System.out.println("================================");               for(Difference departure : differences){             System.out.println(difference);         }     } }

You tin come across that nosotros are outset printing total expose of differences betwixt 2 XML files together with and then printing each unlike past times going through the List which contains each Difference.

10 Essential tools for Java Programmers.

one trouble XML file for comparison. Now our target.xml volition facial expression like

<employees>  <employee id="1">    <name>James</name>      <department>Sales</department>    <phone>9843267462</phone>     </employee> </employees>

Output:
=============================== Total differences : 13 ================================ Expected expose of modest fry nodes '3' but was '1' -  comparing at /employees[1] to at /employees[1]

All these differences spell comparing origin together with target XML files comes because of white infinite together with they are non truthful differences.

Since inward real-world scenario, it's ever possible to compare 2 XML files alongside a departure inward whitespace, it's best to ignore white infinite spell comparing XML files together with thankfully XMLUnit tin endure configured to ignore whitespace past times using static method XMLUnit.setIgnoreWhitespace(true).

Now if you lot re-run this Java computer program after adding a telephone hollo upwards to XMLUnit.setIgnoreWhitespace(true), alongside the same input, you lot volition in 1 lawsuit once to a greater extent than come across a unmarried departure every bit shown below.

=============================== Total differences : 1 ================================ Expected text value '8034832190' but was '9843267462

Similarly you lot tin also ignore comments past times calling XMLUnit.setIgnoreComments(true) earlier comparing XML files inward Java.  You tin fifty-fifty piece of job overloaded method XMLUnit.compareXML(source, target) to compare 2 XML files. This method takes Document, Reader, or String to acquire XML content.


That's all on how to compare 2 XML files inward Java using XMLUnit example. XMLUnit library is rich together with powerful together with provides several other options to compare XML documents including differences inward XPath, comparing transformations etc.

XMLUnit library tin also endure used every bit JUnit past times extending XMLTestCase class, which provides methods similar assertXMLEqual(org.w3c.dom.Document source, org.w3c.dom.Document target) together with several other overloaded versions for testing XML files. You tin banking company stand upwards for if an XPath exists or non together with whether it incorporate the expected value or not. It's a rattling skillful tool to write automated tests if your computer program is generating or modifying XML files.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services together with REST API alongside Spring Boot
answer)
  • How to format dates spell converting XML to Java using JAXB? (example)
  • Step past times Step guide to parsing XML using SAX parser inward Java? (tutorial)
  • How to read XML file using DOM Parser inward Java? (tutorial)
  • How to escape XML Special grapheme inward Java String? (tutorial)
  • How to parse XML document using JDOM Parser inward Java? (tutorial)
  • How to exercise together with evaluate XPath Expressions inward Java? (guide)
  • Top 10 XSLT Transformation Interview Questions? (FAQ)
  • Top 10 XML Interview Questions for Java Programmers? (FAQ)

  • Thanks for reading this tutorial hence far, if you lot actually similar this tutorial together with then delight similar our Facebook page together with don't forget to percentage alongside your friends together with colleagues. If you lot receive got whatever proposition or feedback together with then delight drib a comment. If you lot merely desire to exercise 1 affair at the moment, together with then read Test Driven to larn to a greater extent than practical together with automated ways to bear witness your applications. 

    Sumber https://javarevisited.blogspot.com/

    0 Response to "How To Compare 2 Xml Files Inwards Coffee - Xmlunit Example"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel