XML text file to response Object

Hi,

For one of my soap Request I get a response with special characters for < and > signs.
Because the response has these special characters, I could not run command like WS.getElementPropertyValue and get the correct value.
So I googled and found a way to change special chars to < > signs and also changed the file in pretty format. My question is now how do I go back from this
xml text file to response Object so I can run the command WS.getElementPropertyValue?

Thanks in advance

SOAP messages carry XML documents. A XML document contains a lot of < and > characters. In XML, < and > are nothing special.

I do not understand what you wrote. Please show us more info: your XML data, your test script, error log, etc.

1 Like

Hi again,

Thank you for a quick response.

If the response didn’t have any special characters, I will do the following to get the value of Success Tag.

def locator1 = “GetUploadFeedbackResponse.Return.Transaction_Summary.Success”
response = findTestObject(‘ABC/GetTransactions’)))
String strMessageResponse = WS.getElementPropertyValue(response, locator1)

Here’s a response after I replace the special characters with <>. However this is in a text file test.xml now.
How do I get the <Success? tag value from the text file. 0

  <?xml version="1.0" encoding="UTF-8"?><soap:Envelope 
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body> 
     <dex420c:GetUploadFeedbackResponse xmlns:dex420c="http://metadata.dod.mil/mdr/ns/NECC-AP/FPJ-JDS/1.0.1.6/dex"> 
          <Return> <DEX_Feedback>
                     <AsOf>2022-06-15T13:14:43</AsOf>                        
                     <Transaction_Summary>
                          <Total>1</Total>
                          <Pending>1</Pending>
                          <Success>0</Success>
                          <Success_with_warnings>0</Success_with_warnings>
                          <Rejected>0</Rejected>
                     </Transaction_Summary>
                </DEX_Feedback>
           </Return> 
     </dex420c:GetUploadFeedbackResponse> 
    </soap:Body>
 </soap:Envelope>

@mqamar I can’t speak to your existing code but I can tell you I find dealing with XML much easier using GPathResult.

More info here:

https://www.baeldung.com/groovy-xml

Web search:

https://duckduckgo.com/?t=ffab&q=GPathResult&atb=v267-1&ia=web

Still I do not understand your problem.

Please show us the raw response BEFORE you do any character replacement.

Here’s my raw response before I removed the special characters.

<?xml version="1.0" encoding="UTF-8"?>
 <soap:Body>
      <dex420c:GetUploadFeedbackResponse xmlns:dex420c="http://metadata.dod.mil/mdr/ns/NECC-AP/FPJ-JDS/1.0.1.6/dex">
           <Return>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;DEX_Feedback&gt;&lt;AsOf&gt;2022-09-23T16:52:38&lt;/AsOf&gt;&lt;DEX_File&gt;&lt;Name_Submitted&gt;20220923.165206.jpestest2.trans.002584.ext&lt;/Name_Submitted&gt;&lt;Processing_Started&gt;20220923/16:52:06&lt;/Processing_Started&gt;&lt;Name_Inside_JOPES&gt;20220923.165206.jpestest2.trans.002584.ext&lt;/Name_Inside_JOPES&gt;&lt;messages&gt;&lt;/messages&gt;&lt;result&gt;DEX conversion completed:  In /1/  Out /1/&lt;/result&gt;&lt;/DEX_File&gt;&lt;Transactions&gt;&lt;Transaction_Info&gt;&lt;jsp_tsn&gt;3333343848&lt;/jsp_tsn&gt;&lt;processing_step&gt;&lt;name&gt;DEX_conversion&lt;/name&gt;&lt;message&gt;NONE&lt;/message&gt;&lt;result&gt;Successful conversion&lt;/result&gt;&lt;/processing_step&gt;&lt;processing_step&gt;&lt;name&gt;Final_application&lt;/name&gt;&lt;message&gt;&lt;/message&gt;&lt;result&gt;SUCCESS&lt;/result&gt;&lt;trans_frag&gt;&lt;/trans_frag&gt;&lt;/processing_step&gt;&lt;/Transaction_Info&gt;&lt;/Transactions&gt;&lt;Transaction_Summary&gt;&lt;Total&gt;1&lt;/Total&gt;&lt;Pending&gt;0&lt;/Pending&gt;&lt;Success&gt;1&lt;/Success&gt;&lt;Success_with_warnings&gt;0&lt;/Success_with_warnings&gt;&lt;Rejected&gt;0&lt;/Rejected&gt;&lt;/Transaction_Summary&gt;&lt;/DEX_Feedback&gt;</Return>
      </dex420c:GetUploadFeedbackResponse>
 </soap:Body>

</soap:Envelope>

def locator1 = “GetUploadFeedbackResponse.Return.Transaction_Summary.Success”
response = findTestObject(‘ABC/GetTransactions’)))
String strMessageResponse = WS.getElementPropertyValue(response, locator1)

I get no results back when I run WS.getElementPropertyValue(response, locator1) command.
Therefore I removed the special characters and replaced them with <> signs. Now back to my original question, “My question is now how do I go back from this
xml text file to response Object so I can run the command WS.getElementPropertyValue?”

Your raw response contains the following <Return> element.

           <Return>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;DEX_Feedback&gt;&lt;AsOf&gt;2022-09-23T16:52:38&lt;/AsOf&gt;&lt;DEX_File&gt;&lt;Name_Submitted&gt;20220923.165206.jpestest2.trans.002584.ext&lt;/Name_Submitted&gt;&lt;Processing_Started&gt;20220923/16:52:06&lt;/Processing_Started&gt;&lt;Name_Inside_JOPES&gt;20220923.165206.jpestest2.trans.002584.ext&lt;/Name_Inside_JOPES&gt;&lt;messages&gt;&lt;/messages&gt;&lt;result&gt;DEX conversion completed:  In /1/  Out /1/&lt;/result&gt;&lt;/DEX_File&gt;&lt;Transactions&gt;&lt;Transaction_Info&gt;&lt;jsp_tsn&gt;3333343848&lt;/jsp_tsn&gt;&lt;processing_step&gt;&lt;name&gt;DEX_conversion&lt;/name&gt;&lt;message&gt;NONE&lt;/message&gt;&lt;result&gt;Successful conversion&lt;/result&gt;&lt;/processing_step&gt;&lt;processing_step&gt;&lt;name&gt;Final_application&lt;/name&gt;&lt;message&gt;&lt;/message&gt;&lt;result&gt;SUCCESS&lt;/result&gt;&lt;trans_frag&gt;&lt;/trans_frag&gt;&lt;/processing_step&gt;&lt;/Transaction_Info&gt;&lt;/Transactions&gt;&lt;Transaction_Summary&gt;&lt;Total&gt;1&lt;/Total&gt;&lt;Pending&gt;0&lt;/Pending&gt;&lt;Success&gt;1&lt;/Success&gt;&lt;Success_with_warnings&gt;0&lt;/Success_with_warnings&gt;&lt;Rejected&gt;0&lt;/Rejected&gt;&lt;/Transaction_Summary&gt;&lt;/DEX_Feedback&gt;</Return>

The <Return> element here is a valid XML element which contains a text string which starts with &lt;?xml and ends with &lt;/DEX_Feedbback&gt;. This response is in a valid format as XML.

If you change your code as follows and ran it:

def response = findTestObject(‘ABC/GetTransactions’)))
String returnedStr = 
    WS.getElementPropertyValue(response, 
                               "GetUploadFeedbackResponse.Return")
println "returnedStr: " + returnedStr 

You will see in the console:

returnedStr: <?xml version="1.0" encoding="UTF-8"?> <DEX_Feedback><AsOf>2022-09-23T16:52:38</AsOf><DEX_File><Name_Submitted>20220923.165206.jpestest2.trans.002584.ext</Name_Submitted><Processing_Started>20220923/16:52:06</Processing_Started><Name_Inside_JOPES>20220923.165206.jpestest2.trans.002584.ext</Name_Inside_JOPES><messages></messages><result>DEX conversion completed:  In /1/  Out /1/</result></DEX_File><Transactions><Transaction_Info><jsp_tsn>3333343848</jsp_tsn><processing_step><name>DEX_conversion</name><message>NONE</message><result>Successful conversion</result></processing_step><processing_step><name>Final_application</name><message></message><result>SUCCESS</result><trans_frag></trans_frag></processing_step></Transaction_Info></Transactions><Transaction_Summary><Total>1</Total><Pending>0</Pending><Success>1</Success><Success_with_warnings>0</Success_with_warnings><Rejected>0</Rejected></Transaction_Summary></DEX_Feedback>

Please note that &lt; is translated to < and &gt; is translated to >. Katalon Studio internally performs parsing the response XML, which results this implicit character translation. Your script does not have to do this character conversions for itself.

Please note also that your Groovy script regards the variable returnedStr as of type java.lang.String. Your script does NOT yet recognizes the returnedStr variable as some object type for XML processing (e.g., org.w3c.dom.Document).

A String is just a string. The WS.getElementPropertyValue() command can not look into a plain String. It can not recognize an XML element <Success>0</Success> inside a string.

So, your Groovy script need to parse a plain String into an object of XML-type. Please read https://www.baeldung.com/groovy-xml to study how to process XML in Groovy.

The following code-snippet would do parsing a String to get an GPathResult and look into its element tree:

GPathResult xmlContent = new XmlSlurper.parse(returnedStr)
println "Success: " + xmlContent.Transaction_Summary.Success
// or may be
// println "Success: " + xmlContent.DEX_Feedback.Transaction_Summary.Success
// I am not sure

This will print

Success: 0

Thank you for the response.
I don’t understand why can’t I use the following for locator to get the value from the tag Success? I’ve used longer than this locator before.
def locator1 = “GetUploadFeedbackResponse.Return.Transaction_Summary.Success”

Thanks

I explained it in my previous post at my best. I have no more words to say.

understood thanks for the response.

Lets try another approach. I understand that I don’t need to remove the special chars, they are nothing but < and > signs. If I remove those chars and replace them with < and > signs and make prettyxml, can I still use the following command. If yes, how do I get the xml file to response object?
WS.getElementPropertyValue(response, “GetUploadFeedbackResponse.Return”)

Please try whatever you want. You will learn from them all.