Read xml values in namespaces/SOAP response using groovy XMLParser

Shaik Nizam Ali

I'm using a groovy file where I used xmlParser to generate XML.Now, I want to get the tag values of the xml.

Here is my code

def rootnode = new XmlParser().parseText(responseXml);

Output

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:creditCard">
    <SOAP-ENV:Body><ns1:creditCardResponse xmlns:ns1="urn:creditCard">
        <return xsi:type="tns:RPResponse">
            <Status xsi:type="xsd:int">0</Status>

        </return>
    </ns1:creditCardResponse>
</SOAP-ENV:Body>

I have tried like rootnode.Status[0].text()

However its not getting. How can I get "Status" value in it? Little confused.

Thanks,

cfrick

Just use the "path" down to the var you are interested. You either have to "quote" the namespaces (that means, use strings as accessors, as chars like : and - would be interpreted by groovy) or use the groovy.xml.Namespace helper. E.g. (see comments):

def xml = new groovy.util.XmlParser().parseText('''\
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:creditCard">
    <SOAP-ENV:Body><ns1:creditCardResponse xmlns:ns1="urn:creditCard">
        <return xsi:type="tns:RPResponse">
            <Status xsi:type="xsd:int">666</Status>

        </return>
    </ns1:creditCardResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
''')

// XXX namespaces quoted
assert xml.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Status.text()=='666'

// XXX access by namespace
def nsSoapEnv = new groovy.xml.Namespace('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV')
def nsNs1 = new groovy.xml.Namespace('urn:creditCard', 'ns1')
assert xml[nsSoapEnv.Body][nsNs1.creditCardResponse].return.Status.text()=='666'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Parsing attributes and Values using Groovy's XmlParser

From Dev

Searching XML element in whole document with Groovy XmlParser

From Dev

Read xml response using php

From Dev

Appending to attribute values of an xml file using Groovy

From Dev

API xml response looping and extract all the element values - Groovy

From Dev

Accessing and updating an attribute that is qualified with a namespace using Groovy XMLParser

From Dev

Accessing and updating an attribute that is qualified with a namespace using Groovy XMLParser

From Dev

How to Read XML Response using Java

From Dev

Change xml element/tag name using XmlSlurper or XmlParser

From Dev

Read values from a complex XML using java

From Dev

Groovy XMLParser - update node text

From Dev

How to compare values from response of two different requests in SOAP UI using groovy?

From Dev

How to compare values from response of two different requests in SOAP UI using groovy?

From Dev

Read XML Response From Page

From Dev

Read xml response in unity Android

From Dev

XML read uncomfortable values

From Dev

XML read and getting values

From Dev

Read attribute names and their values from a xml file in Qt using QDomNodeList

From Dev

Getting NULL values while using OPENXML to read from XML file

From Dev

Can not read values/nodes from xml using c#

From Dev

CSV to XML converter using Groovy

From Dev

CSV to XML converter using Groovy

From Dev

Convert JSON to XML using groovy?

From Dev

create XML using groovy issue

From Dev

Groovy:Xml: How to display Xml response in textarea of gsp page

From Dev

Assert null value in Json response using Groovy

From Dev

Groovy replace node values in xml with xpath

From Dev

Get the values of all children of a XML node in Groovy

From Dev

How to read 2nd element of list of xml items returned in response using ExtractVariables policy in apigee?

Related Related

  1. 1

    Parsing attributes and Values using Groovy's XmlParser

  2. 2

    Searching XML element in whole document with Groovy XmlParser

  3. 3

    Read xml response using php

  4. 4

    Appending to attribute values of an xml file using Groovy

  5. 5

    API xml response looping and extract all the element values - Groovy

  6. 6

    Accessing and updating an attribute that is qualified with a namespace using Groovy XMLParser

  7. 7

    Accessing and updating an attribute that is qualified with a namespace using Groovy XMLParser

  8. 8

    How to Read XML Response using Java

  9. 9

    Change xml element/tag name using XmlSlurper or XmlParser

  10. 10

    Read values from a complex XML using java

  11. 11

    Groovy XMLParser - update node text

  12. 12

    How to compare values from response of two different requests in SOAP UI using groovy?

  13. 13

    How to compare values from response of two different requests in SOAP UI using groovy?

  14. 14

    Read XML Response From Page

  15. 15

    Read xml response in unity Android

  16. 16

    XML read uncomfortable values

  17. 17

    XML read and getting values

  18. 18

    Read attribute names and their values from a xml file in Qt using QDomNodeList

  19. 19

    Getting NULL values while using OPENXML to read from XML file

  20. 20

    Can not read values/nodes from xml using c#

  21. 21

    CSV to XML converter using Groovy

  22. 22

    CSV to XML converter using Groovy

  23. 23

    Convert JSON to XML using groovy?

  24. 24

    create XML using groovy issue

  25. 25

    Groovy:Xml: How to display Xml response in textarea of gsp page

  26. 26

    Assert null value in Json response using Groovy

  27. 27

    Groovy replace node values in xml with xpath

  28. 28

    Get the values of all children of a XML node in Groovy

  29. 29

    How to read 2nd element of list of xml items returned in response using ExtractVariables policy in apigee?

HotTag

Archive