SOAP: detecting nil element in the response

M

Mark Slater

I'm still getting used to Ruby, so there could be a very easy and
obvious answer to this one. Essentially, the server sends back nil
elements, which Ruby translates into SOAP::Mapping::Object instances.
But how do I determine when those SOAP::Mapping::Object instances
represent nil elements?

Here are the details. I'm calling a soap method to get profile
information about a user in the system I'm writing. The soap that
comes back looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<getUserInformationResponse xmlns="http://rpc.whisper.ucsc.edu">
<out xmlns="http://rpc.whisper.ucsc.edu">
<aboutText xmlns="http://core.whisper.ucsc.edu">This is stuff
about me.</aboutText>
<familyName xmlns="http://core.whisper.ucsc.edu">Last</familyName>
<firstName xmlns="http://core.whisper.ucsc.edu">First</firstName>
<gender xmlns="http://core.whisper.ucsc.edu">0</gender>
<namePrefix xmlns="http://core.whisper.ucsc.edu">Dr.</namePrefix>
<nameSuffix xmlns="http://core.whisper.ucsc.edu">Esq</nameSuffix>
<otherNames xmlns="http://core.whisper.ucsc.edu"/>
<previousNames xmlns="http://core.whisper.ucsc.edu">
<ns1:string xmlns:ns1="http://
rpc.whisper.ucsc.edu">previous_name_1</ns1:string>
<ns1:string xmlns:ns1="http://
rpc.whisper.ucsc.edu">previous_name_2</ns1:string>
</previousNames>
</out>
</getUserInformationResponse>
</soap:Body>
</soap:Envelope>

The WSDL (auto generated by XFire) for this particular type is:

<xsd:complexType name="UserInfoData">
<xsd:sequence>
<xsd:element name="aboutText" type="xsd:string" minOccurs="0"
nillable="true"/>
<xsd:element name="familyName" type="xsd:string" minOccurs="0"
nillable="true"/>
<xsd:element name="firstName" type="xsd:string" minOccurs="0"
nillable="true"/>
<xsd:element name="gender" type="xsd:string" minOccurs="0"
nillable="true"/>
<xsd:element name="namePrefix" type="xsd:string" minOccurs="0"
nillable="true"/>
<xsd:element name="nameSuffix" type="xsd:string" minOccurs="0"
nillable="true"/>
<xsd:element name="otherNames" type="xsd:string" minOccurs="0"
nillable="true"/>
<xsd:element name="previousNames" type="tns:ArrayOfString"
minOccurs="0" nillable="true"/>
</xsd:sequence>
</xsd:complexType>

You'll notice that the otherNames element in the response is nil and
that its typed as a string in the WSDL. Users are not required to
enter any of this data, so having nil elements in the response is
valid semantically. In trying to figure out how Ruby SOAP clients
work, I'm doing the following when I get the results back:

...
results = driver.getUserInformation( :in0 => user )
puts '----- result data -----'
puts "name: #{results.out.namePrefix} #{results.out.firstName} #
{results.out.otherNames} #{results.out.familyName}, #
{results.out.nameSuffix}"
puts "gender: #{results.out.gender}"
puts "about text: #{results.out.aboutText}"

puts "first name type = #{results.out.firstName.class}"
puts "other names type = #{results.out.otherNames.class}"
puts "previous names type = #{results.out.previousNames.class}"

The output of that code gives me:

----- results -----
#<SOAP::Mapping::Object:0xa87a5a {http://rpc.whisper.ucsc.edu}
out=#<SOAP::Mapping::Object:0xa87924 {http://core.whisper.ucsc.edu}
aboutText="This is stuff about me." {http://core.whisper.ucsc.edu}
familyName="Last" {http://core.whisper.ucsc.edu}
firstName="First" {http://core.whisper.ucsc.edu}gender="0" {http://
core.whisper.ucsc.edu}namePrefix="Dr." {http://core.whisper.ucsc.edu}
nameSuffix="Esq" {http://core.whisper.ucsc.edu}
otherNames=#<SOAP::Mapping::Object:0xa86a60> {http://
core.whisper.ucsc.edu}previousNames=#<SOAP::Mapping::Object:0xa85f84
{http://rpc.whisper.ucsc.edu}string=["previous_name_1",
"previous_name_2"]>>>
----- result data -----
name: Dr. First #<SOAP::Mapping::Object:0xa86a60> Last, Esq
gender: "0"
about text: "This is stuff about me."
first name type = String
other names type = SOAP::Mapping::Object
previous names type = SOAP::Mapping::Object

Obviously if the element is assigned to an object, I can't just test
to see if its nil; I've also tried comparing it to an empty string.
Is there any way to do detect a nil element in the SOAP response?

Thanks,

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top