views:

162

answers:

1

I have a web service that returns following type:

<xsd:complexType name="TaggerResponse">
    <xsd:sequence>
     <xsd:element name="msg" type="xsd:string"></xsd:element>
    </xsd:sequence>
    <xsd:attribute name="status" type="tns:Status"></xsd:attribute>
</xsd:complexType>

The type contains one element (msg) and one attribute (status).

To communicate with the web service I use SOAPpy library. Below is a sample result return by the web service (SOAP message):

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
   <SOAP-ENV:Body>
      <SOAP-ENV:TagResponse>
         <parameters status="2">
            <msg>text</msg>
         </parameters>
      </SOAP-ENV:TagResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Python parses this message as:

<SOAPpy.Types.structType parameters at 157796908>: {'msg': 'text'}

As you can see the attribute is lost. What should I do to get the value of "status"?

+1  A: 

The response example you posted (the actual XML coming back from the WS request) does not have the value in it you are looking for! I would suggest this is why SOAPpy cannot return it to you.

If it is a case of making your code have consistent behaviour in cases where the value is returned and when it isn't then try using dict's get() method to get the value:

attribute_value = result.get("attribute", None)

Then you can test the result for none. You can also do so like this:

if not "attribute" in result:
    ...handle case where there is no attribute value...
jkp
I want to the the value of "status" that is in the message. The `get` method does not work for the result: `structType instance has no attribute 'get'`. However, you answer gave me a clue and I found out that using `r._getAttr("status")` the value I am looking for. But, ... `_getAttr` is a internal function and can I use it or there is more appropriate way to get the value?
czuk
czuk: if the class inherits from object you can use the getattr() builtin. For example getattr(result, "status", None) will return the value of status in the result, or None if it was not set.
jkp
The function `getattr` does not exists as well. I wonder if it might be a problem with WSDL declaration. One of the validators says that a namespace attribute is missing. I am working on that.
czuk
Try introspecting the results instance using the `dir()` function: this will tell you what attributes it has.
jkp