1

Iam new to this field and has been assigned with a challenging task. I have 2 XML files (namely Test1 & Test 2 as mentioned below). And i need to compare these xml files using java and return boolean value as true if there are no differences.And the challenge here is the Second Xml file (Test2.xml) the elements order is always jumbled. I tried XMLUnit2 for comparing the 2 xml strings but it is failing if there are multiple parent nodes or large xml.

Test1.Xml:

Test1.Xml:

<PACKINGS>
    <PACKING>
      <TYPE>CCST</TYPE>
      <ORDERNUM>810000510</ORDERNUM>
      <SVCTAGS>
        <SVCTAG>
          <SVCTAGTYPE>DRAGON</SVCTAGTYPE>
          <SVCTAGNUMBER>768100005105001</SVCTAGNUMBER>
          <TIENUMBER>1</TIENUMBER>
          <BOXID>768100005105001</BOXID>
          <LENGTH>4</LENGTH>
          <WIDTH>5</WIDTH>
          <HEIGHT>10</HEIGHT>
          <PARTS>
            <PART>
              <PARTNUMBER>RKH5D</PARTNUMBER>
              <PARTQTY>10</PARTQTY>
            </PART>
          </PARTS>
        </SVCTAG>
        <SVCTAG>
          <SVCTAGTYPE>DRAGON</SVCTAGTYPE>
          <SVCTAGNUMBER>768100005105002</SVCTAGNUMBER>
          <TIENUMBER>2</TIENUMBER>
          <BOXID>768100005105002</BOXID>
          <LENGTH>4</LENGTH>
          <WIDTH>5</WIDTH>
          <HEIGHT>10</HEIGHT>
          <PARTS>
            <PART>
              <PARTNUMBER>FHMTN</PARTNUMBER>
              <PARTQTY>10</PARTQTY>
            </PART>
          </PARTS>
        </SVCTAG>
      </SVCTAGS>
    </PACKING>
  </PACKINGS>

Test2.Xml:

  <PACKINGS>
      <PACKING>
        <TYPE>CCST</TYPE>
        <ORDERNUM>810000510</ORDERNUM>
        <SVCTAGS>
        <SVCTAG>
        <SVCTAGTYPE>DRAGON</SVCTAGTYPE>
        <SVCTAGNUMBER>768100005105002</SVCTAGNUMBER>
        <TIENUMBER>2</TIENUMBER>
        <BOXID>768100005105002</BOXID>
        <LENGTH>4</LENGTH>
        <WIDTH>5</WIDTH>
        <HEIGHT>10</HEIGHT>
      <PARTS>
        <PART>
         <PARTNUMBER>FHMTN</PARTNUMBER>
         <PARTQTY>10</PARTQTY>
         </PART>
       </PARTS>
      </SVCTAG>
      <SVCTAG>
        <SVCTAGTYPE>DRAGON</SVCTAGTYPE>
        <SVCTAGNUMBER>768100005105001</SVCTAGNUMBER>
        <TIENUMBER>1</TIENUMBER>
        <BOXID>768100005105001</BOXID>
        <LENGTH>4</LENGTH>
        <WIDTH>5</WIDTH>
        <HEIGHT>10</HEIGHT>
      <PARTS>
       <PART>
        <PARTNUMBER>RKH5D</PARTNUMBER>
        <PARTQTY>10</PARTQTY>
        </PART>
      </PARTS>
     </SVCTAG>
    </SVCTAGS>
   </PACKING>
  </PACKINGS>

Below is the code that i tried but it is failing using xml unit:

package com.com.java;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;

import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLAssert;
import org.testng.annotations.Test;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.ElementSelectors;
import org.xmlunit.matchers.CompareMatcher; 

public class TestNg  {
 @Test
 public void testXmlUnit() {
  String ControlXML = "<PACKINGS><PACKING><TYPE>CCST</TYPE><ORDERNUM>810000510</ORDERNUM><SVCTAGS><SVCTAG><SVCTAGTYPE>DRAGON</SVCTAGTYPE><SVCTAGNUMBER>768100005105002</SVCTAGNUMBER><TIENUMBER>2</TIENUMBER><BOXID>768100005105002</BOXID><LENGTH>4</LENGTH><WIDTH>5</WIDTH><HEIGHT>10</HEIGHT></SVCTAG><SVCTAG><SVCTAGTYPE>DRAGON</SVCTAGTYPE><SVCTAGNUMBER>768100005105001</SVCTAGNUMBER><TIENUMBER>1</TIENUMBER><BOXID>768100005105001</BOXID><LENGTH>4</LENGTH><WIDTH>5</WIDTH><HEIGHT>10</HEIGHT><PARTS><PART><PARTNUMBER>RKH5D</PARTNUMBER><PARTQTY>10</PARTQTY></PART></PARTS></SVCTAG></SVCTAGS></PACKING></PACKINGS>";
     String testXml = "<PACKINGS><PACKING><TYPE>CCST</TYPE><ORDERNUM>810000510</ORDERNUM><SVCTAGS><SVCTAG><SVCTAGTYPE>DRAGON</SVCTAGTYPE><SVCTAGNUMBER>768100005105001</SVCTAGNUMBER><TIENUMBER>1</TIENUMBER><BOXID>768100005105001</BOXID><LENGTH>4</LENGTH><WIDTH>5</WIDTH><HEIGHT>10</HEIGHT></SVCTAG><SVCTAG><SVCTAGTYPE>DRAGON</SVCTAGTYPE><SVCTAGNUMBER>768100005105002</SVCTAGNUMBER><TIENUMBER>2</TIENUMBER><BOXID>768100005105002</BOXID><LENGTH>4</LENGTH><WIDTH>5</WIDTH><HEIGHT>10</HEIGHT><PARTS><PART><PARTNUMBER>FHMTN</PARTNUMBER><PARTQTY>10</PARTQTY></PART></PARTS></SVCTAG></SVCTAGS></PACKING></PACKINGS>";
     assertThat(testXml, CompareMatcher.isSimilarTo(ControlXML).ignoreWhitespace().normalizeWhitespace().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText, ElementSelectors.byName)));

}  
 }
user988904
  • 31
  • 4

2 Answers2

0

I am assuming you are getting both files as String.

ex. String xml1 = .. , String xml2 = ..

You can try below options :

1) You can parse both Strings and compare each elements one by one

2) you can use XMLUnit - https://www.xmlunit.org/

Raj
  • 707
  • 6
  • 23
0

All off-the-shelf methods that I know of for comparing XML documents treat element order as signficant. I think you're going to have to devise a transformation to these documents that eliminates the irrelevant differences by normalizing them to a common form, and then compare the normalized forms. The obvious way to do this transformation is in XSLT.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164