How To Compare Ii Xml Files Inwards Coffee - Xmlunit Example
The XMLUnit library tin survive used to compare ii XML files inward Java. Similar to JUnit, XMLUnit tin also survive used to essay out XML files for comparing yesteryear 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(), equally ii XML which contains unlike comment together with whitespace tin survive equals, which is non truthful for String or grapheme comparison. Also spell comparing XML files, it's really of import to know precisely which content or business office is unlike together with XMLUnit non alone shows the content which is unlike but also XPath of elements which is getting compared.
The pump together with individual of XMLUnit are the DifferenceEngine flat but nosotros won't role it directly. Instead, yous volition role Diff together with DetailedDiff the ii of import classes for XML comparison. They supply comparing engine for comparing XML. The XMLUnit library non alone compares consummate XML document but also tin perform a lot of useful activities related to XPath e.g. it tin cheque if an XPATH exists or non exists. It tin fifty-fifty cheque if XPath contains expected value or not.
The XMLUnit library tin also supply validation support. So, if yous desire to cheque if an XML file confirms to DTD or not, yous tin practice that using XMLUnit's Validator class. The default validation is using DTD but if yous desire to confirm against XSD schema, yous tin practice that yesteryear using selection Validator.useXMLSChema flag.
Here is consummate Java programme to compare ii XML documents using XMLUnit library. In this example, nosotros receive got ii XML files source.xml together with target.xml, afterwards is created yesteryear copying the sometime file together with I receive got made 1 change, inward a telephone set out to compare these ii XML files. In gild to compare together with demo differences betwixt XML documents, I receive got created ii static methods compareXML() together with printDifferences().
The code to read XML file is really similar to the code of reading text files inward Java, nosotros are reading XML files equally InputStream together with passing it to compareXML() equally Reader object. Real XML comparing begins inward this method alongside Diff together with DetailedDiff class.
The DetailedDiff render all differences equally List, if in that place is no divergence than the size of this List would survive nil together with nosotros tin enjoin ii XML files are identical inward data. printDifference() method takes this List of Differences together with prints it on Console. If yous human face divergence provided yesteryear XMLUnit, yous volition discovery that it shows both what is the divergence together with where is that divergence come about yesteryear showing consummate XPATH.
Java Program to compare XML files
You tin come across that nosotros are outset printing total set out of differences betwixt ii XML files together with thence printing each unlike yesteryear going through the List which contains each Difference.
The pump together with individual of XMLUnit are the DifferenceEngine flat but nosotros won't role it directly. Instead, yous volition role Diff together with DetailedDiff the ii of import classes for XML comparison. They supply comparing engine for comparing XML. The XMLUnit library non alone compares consummate XML document but also tin perform a lot of useful activities related to XPath e.g. it tin cheque if an XPATH exists or non exists. It tin fifty-fifty cheque if XPath contains expected value or not.
The XMLUnit library tin also supply validation support. So, if yous desire to cheque if an XML file confirms to DTD or not, yous tin practice that using XMLUnit's Validator class. The default validation is using DTD but if yous desire to confirm against XSD schema, yous tin practice that yesteryear using selection Validator.useXMLSChema flag.
Java programme to compare ii 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 yous desire to role to a greater extent than advanced XPath engine based upon JAXP 1.3 thence yous demand to include Java 1.5 or higher version inward the classpath. In this Java together with XML tutorial, yous volition larn how to compare ii XML documents inward Java yesteryear next a uncomplicated 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 programme to compare ii XML documents using XMLUnit library. In this example, nosotros receive got ii XML files source.xml together with target.xml, afterwards is created yesteryear copying the sometime file together with I receive got made 1 change, inward a telephone set out to compare these ii XML files. In gild to compare together with demo differences betwixt XML documents, I receive got created ii static methods compareXML() together with printDifferences().
The code to read XML file is really similar to the code of reading text files inward Java, nosotros are reading XML files equally InputStream together with passing it to compareXML() equally Reader object. Real XML comparing begins inward this method alongside Diff together with DetailedDiff class.
The DetailedDiff render all differences equally List, if in that place is no divergence than the size of this List would survive nil together with nosotros tin enjoin ii XML files are identical inward data. printDifference() method takes this List of Differences together with prints it on Console. If yous human face divergence provided yesteryear XMLUnit, yous volition discovery that it shows both what is the divergence together with where is that divergence come about yesteryear 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 programme to compare ii XML files using XMLUnit illustration * @author Javin Paul */ public class XMLComparator { public static void main(String args[]) throws FileNotFoundException, SAXException, IOException { // reading ii 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 ii XML using XMLUnit inward Java List differences = compareXML(source, target); //showing differences flora inward ii xml files printDifferences(differences); } public static List compareXML(Reader source, Reader target) throws SAXException, IOException{ //creating Diff instance to compare ii XML files Diff xmlDiff = new Diff(source, target); //for getting detailed differences betwixt ii 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 divergence : differences){ System.out.println(difference); } } }
You tin come across that nosotros are outset printing total set out of differences betwixt ii XML files together with thence printing each unlike yesteryear going through the List which contains each Difference.
10 Essential tools for Java Programmers.
one draw of piece of occupation XML file for comparison. Now our target.xml volition human face like
Output:
All these differences spell comparing origin together with target XML files comes because of white infinite together with they are non truthful differences.
<employees> <employee id="1"> <name>James</name> <department>Sales</department> <phone>9843267462</phone> </employee> </employees> Output:
=============================== Total differences : 13 ================================ Expected set out of nipper 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 ii XML files alongside a divergence inward whitespace, it's best to ignore white infinite spell comparing XML files together with thankfully XMLUnit tin survive configured to ignore whitespace yesteryear using static method XMLUnit.setIgnoreWhitespace(true).
Now if yous re-run this Java programme after adding a telephone band to XMLUnit.setIgnoreWhitespace(true), alongside the same input, yous volition in 1 lawsuit once to a greater extent than come across a unmarried divergence equally shown below.
Similarly yous tin also ignore comments yesteryear calling XMLUnit.setIgnoreComments(true) earlier comparing XML files inward Java. You tin fifty-fifty role overloaded method XMLUnit.compareXML(source, target) to compare ii XML files. This method takes Document, Reader, or String to acquire XML content.
That's all on how to compare ii 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 survive used equally JUnit yesteryear 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 cheque if an XPath exists or non together with whether it incorporate the expected value or not. It's a really skillful tool to write automated tests if your programme 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 yesteryear 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 practice 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 thence far, if yous actually similar this tutorial thence delight similar our Facebook page together with don't forget to portion alongside your friends together with colleagues. If yous receive got whatever proposition or feedback thence delight drib a comment. If yous simply desire to practice 1 affair at the moment, thence read Test Driven to larn to a greater extent than practical together with automated ways to essay out your applications.
Now if yous re-run this Java programme after adding a telephone band to XMLUnit.setIgnoreWhitespace(true), alongside the same input, yous volition in 1 lawsuit once to a greater extent than come across a unmarried divergence equally shown below.
=============================== Total differences : 1 ================================ Expected text value '8034832190' but was '9843267462
Similarly yous tin also ignore comments yesteryear calling XMLUnit.setIgnoreComments(true) earlier comparing XML files inward Java. You tin fifty-fifty role overloaded method XMLUnit.compareXML(source, target) to compare ii XML files. This method takes Document, Reader, or String to acquire XML content.
That's all on how to compare ii 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 survive used equally JUnit yesteryear 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 cheque if an XPath exists or non together with whether it incorporate the expected value or not. It's a really skillful tool to write automated tests if your programme 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)
Thanks for reading this tutorial thence far, if yous actually similar this tutorial thence delight similar our Facebook page together with don't forget to portion alongside your friends together with colleagues. If yous receive got whatever proposition or feedback thence delight drib a comment. If yous simply desire to practice 1 affair at the moment, thence read Test Driven to larn to a greater extent than practical together with automated ways to essay out your applications.
Komentar
Posting Komentar