2

I need to compare an XML file with that of Json response. Below is just a short version of original xml file, JSON and XML have different attribute names because of which i cannot compare them directly. I need to call function to format xml so that it can be compared with json. During this process, I am getting the error Cannot cast java.util.LinkedHashMap to java.util.List after running below code.

* xml list = $Test1/Body
* print list

* def xpath = function(x, p){ try { return karate.xmlPath(x, p) } catch (e) { return '#notpresent' } }
* def fun = function(x){ return { code: xpath(x, '/Body/code') } }
* def temp = karate.map(list, fun)
* print temp

Test1 is an xml file containing below sample data;

<ns9:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns5:plan">
  <ns4:code>XPBSMWAT</ns4:code>
</ns9:Body>

This is a simpler version of XML data; my actual xml file is bigger.

sn1693
  • 271
  • 2
  • 21

1 Answers1

2

karate.map takes 2 arguments list and a function,

From your question,

* xml list = $Test1/Body

variable name list is actually of a xml data type as you defined, not a list.

karate takes XML/JSON as java.util.LinkedHashMap since you passed the XML as input to the map function it tried to cast it into List and failed.

So it should be something like,

* def list = <something>

But make sure instead of xml it returns a list

Your xml path $Test1/Body returns xml

when you print the list you will see it surrounded with [ and ]

I am not quite sure what you are trying to do with this xml and JS functions but this could be root cause for the error you are getting.

Community
  • 1
  • 1
Babu Sekaran
  • 4,129
  • 1
  • 9
  • 20
  • Thanks Babu for the informative answer, but I had tried this method as well. Still i get the same error. BTW, I added some more details in question. – sn1693 Dec 19 '18 at 08:46