# SOAP: detecting nil element in the response

**URL:** <https://www.ruby-forum.com/t/soap-detecting-nil-element-in-the-response/81044>\
**Category:** Ruby\
**Created:** [January 3, 2007, 9:43am UTC](https://www.ruby-forum.com/t/soap-detecting-nil-element-in-the-response/81044 "2007-01-03T09:43:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [January 3, 2007, 9:43am UTC](https://www.ruby-forum.com/t/soap-detecting-nil-element-in-the-response/81044/1 "2007-01-03T09:43:39Z")

</div>

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/](http://schemas.xmlsoap.org/soap/envelope/)”  
xmlns:xsd=“[http://www.w3.org/2001/XMLSchema](http://www.w3.org/2001/XMLSchema)” xmlns:xsi=“http://  
[www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)”\>  
soap:Body  
  
  
This is stuff  
about me.  
Last  
First  
0  
Dr.  
Esq  
  
  
\<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\>  
  
  
  
\</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](http://rpc.whisper.ucsc.edu)}  
out=#\<SOAP::Mapping::Object:0xa87924 {[http://core.whisper.ucsc.edu](http://core.whisper.ucsc.edu)}  
aboutText=“This is stuff about me.” {[http://core.whisper.ucsc.edu](http://core.whisper.ucsc.edu)}  
familyName=“Last” {[http://core.whisper.ucsc.edu](http://core.whisper.ucsc.edu)}  
firstName=“First” {[http://core.whisper.ucsc.edu](http://core.whisper.ucsc.edu)}gender=“0” {http://  
core.whisper.ucsc.edu}namePrefix=“Dr.” {[http://core.whisper.ucsc.edu](http://core.whisper.ucsc.edu)}  
nameSuffix=“Esq” {[http://core.whisper.ucsc.edu](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](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
