-1

I am trying to compare XML reponse of 2 REST API's. I wanted to compare few fields from Response1 to Response2.

Response1:

<d:site_name>Bondi Junction</d:site_name>
<d:country>AU</d:country>
<d:regio>NSW</d:regio>
<d:contact>123456789</d:contact>

Response2:

<d:country>AU</d:country>
<d:region>NSW</d:region>

I have created a Collection which will have both API's and I wanna run both API's and compare the available columns ( I don't want to compare completely).

Could you please guide me an approach to done.

Thanks, Sekar

enter image description here

rksekar
  • 3
  • 5
  • Try this [XmlXdiff](https://pypi.org/project/XmlXdiff/) python library. But attention it is currently under development. Here is an [example diff](https://i.stack.imgur.com/yR8c8.png) concerning your problem. –  Jan 06 '19 at 21:22

1 Answers1

1

As far as I know, there's no easy way to compare values of xml attributes and elements without converting the xml to json.

Below is a working example(xml converted to json) for 2 use-cases (test)

  1. Using the function difference(), you can test if the 2 xmls have any differences.
  2. Using json path to extract the value of xml elements you can also compare specific field values

    function difference(object, base) {
        function changes(object, base) {
            return _.transform(object, function(result, value, key) {
                if (!_.isEqual(value, base[key])) {
                    result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
                }
            });
        }
        return changes(object, base);
    }
    
    
    var xml1 = `<?xml version=\"1.0\" encoding=\"UTF-8\"?>
               <envelope>
                <body>
                    <d:site_name>Bondi Junction</d:site_name>
                    <d:country>AU</d:country>
                    <d:regio>NSW</d:regio>
                    <d:contact>123456789</d:contact>
                </body>
               </envelope>
    `;
    var xml2 = `<?xml version=\"1.0\" encoding=\"UTF-8\"?>
                <envelope>
                 <body>
                    <d:country>AU</d:country>
                    <d:region>NSW</d:region>
                 </body>
               </envelope>
    `;
    
    var jsonObject1 = xml2Json(xml1);
    var jsonObject2 = xml2Json(xml2);
    var diff = difference(jsonObject1, jsonObject2);
    
    console.log(diff);
    
    pm.test("Test for any differences in the 2 payloads", function() {
        //diff object will be empty in case of no differences
        pm.expect(_.isEmpty(diff)).to.eql(true);
    
    });
    
    
    pm.test("Test for the value of country in 2 payloads ", function() {
        pm.expect(jsonObject1.envelope.body.country).to.eql(jsonObject2.envelope.body.country);
    });
    
Anant Naugai
  • 538
  • 4
  • 14