user1712484
user1712484

Reputation: 65

How to extract classes from SOAP XML via PHP?

Im new to SOAP UI, so is there any method to extract key->value pairs from SOAP UI. I.e. reading "index" of what SOAP interface has to offer? Like reading MIB from SNMP?

For example I can request:

<SOAP:Body>
    <find xmlns="xmlapi">
            <fullClassName>Persons</fullClassName>
            <resultFilter class="Persons.Data">
            <attribute>Name</attribute>
            </resultFilter>
    </find>
 </SOAP:Body>

Class name "Persons" is what I know, but is there a way to retrieve list of "classes" SOAP UI has to offer?

Upvotes: 0

Views: 254

Answers (1)

albciff
albciff

Reputation: 18507

If you want to get all <fullClassName> elements inside <find> for a specific request, a possible way is for example using XmlSlurper inside a groovy testStep:

// get your response 
def response = context.expand( '${TestRequest#Response}' )
// parse it
def xml = new XmlSlurper().parseText(response)
// find all `<fullClassName>` in your xml
def classNames = xml.'**'.findAll { it.name() == 'fullClassName' }
// print all values
classNames.each{
    log.info "fullClassName: $it"
}

As you are new to SOAPUI (maybe also to Groovy) here are some hints:

context.expand( '${TestRequestName#Property}' ) get the content of the specific property from some scoped element. In your case you've to specify your request name and as a property the Response. For more info see property expansion documentation

Groovy automatically use it as a variable for the closures. This is why I use it in each and in findAll.

UPDATE

If you want to know all possible values supported by <fullClassName> you've the follow options:

  1. Check if the type for <fullClassName> defined in the schema has a <xs:restiction> with <xs:enumeration> with the possible values.
  2. If in the schema the type it's simply an <xs:string> or other type which doesn't give you any clue about the allowed value, contact the provider to see an alternative like if there is another SOAP service which returns the values...

For the first case if you've the .xsd try adding a groovy testStep to parse the .xsd and get the <xs:enumeration> values, see the follow example:

def xsd = '''<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<simpleType name="fullClassNameType">
    <restriction base="string">
        <enumeration value="Persons"/>
        <enumeration value="AnotherClassName"/>
        <enumeration value="AnotherOne"/>
    </restriction>
</simpleType>
</schema>'''

// parse the xsd 
def xml = new XmlSlurper().parseText( xsd )
// find your type by name attribute
def fullClassNameType = xml.depthFirst().find { it.@name == 'fullClassNameType' }
// get an array with value attribute of enumeration elements
def allowedValues = fullClassNameType.restriction.enumeration*.@value
log.info allowedValues // [Persons, AnotherClassName, AnotherOne]

Hope it helps,

Upvotes: 1

Related Questions