srok / pysimplesoap

Automatically exported from code.google.com/p/pysimplesoap
0 stars 0 forks source link

Access XML attributes in WSDL responses #117

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The return type of a request if the Client has been instantiated with a wsdl is 
nested dicts/lists. It describes well tags and text content of xml tags. But 
the xml attributes are left aside.

In some cases, information is contained in attributes (ex: Zimbra API define a 
lot of "a" tags, meaningless without their attributes).

What is the way according to pysimplesoap in such a case ?

Regards :)

Joclyn

Original issue reported on code.google.com by joce...@crapouillou.net on 23 Sep 2013 at 7:34

GoogleCodeExporter commented 9 years ago
Handling attributes is a known issue, but how should we map them to python 
types?
What do you suggest?

BTW, you can use the raw client.xml_response and parse it with SimpleXMLElement 
and then access the tag/attributes directly:

xml = SimpleXMLElement(client.xml_response)
print xml('tag')['attribute']

https://code.google.com/p/pysimplesoap/wiki/SimpleXmlElement

For testing, can you attach a sample wsld, xml_request and xml_response?

Original comment by reingart@gmail.com on 23 Sep 2013 at 1:42

GoogleCodeExporter commented 9 years ago
I'm having the same issue right now -- missing xml attributes.

What about to map attributes to a python list of dicts in this way:

{ 'tag': [ {'first_attribute': first_attribute_value}, {'second_attribute': 
'second_attribute_value} ...] ...}

Ok, this is only my 0.5 eurocents on this topic...

Thanks,

Simone

Original comment by simone.fabris on 24 Sep 2013 at 4:40

GoogleCodeExporter commented 9 years ago
I'm no longer using WSDL but trying to provide help for this issue.

The WSDL I use is from http://marsorange.com/files/zimbra_admin-example.wsdl

http://files.zimbra.com/docs/soap_api/8.0.4/soap-docs-804/api-reference/zimbraAd
min/GetAllDomains.html

Sample request is :

    <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:Header>
        <context xmlns="urn:zimbra">
          <authToken xsi:type="xsd:string">0_e65a1STRIPPED6272613b
          </authToken>
          <sessionId xsi:null="1"/>
        </context>
      </soap:Header>
      <soap:Body>
        <GetAllDomainsRequest xmlns="urn:zimbraAdmin">
        </GetAllDomainsRequest>
      </soap:Body>
    </soap:Envelope>

and sample response is :

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Header>
        <context xmlns="urn:zimbra">
          <change token="STRIPPED"/>
        </context>
      </soap:Header>
      <soap:Body>
        <GetAllDomainsResponse xmlns="urn:zimbraAdmin">
          <domain id="b37dSTRIPPEDecf6ac" name="client1.unbound.example.net">
            <a n="zimbraGalLdapPageSize">1000</a>
            <a n="zimbraAggregateQuotaLastUsage">401</a>
            <a n="zimbraInternalSharingCrossDomainEnabled">TRUE</a>
            <a n="zimbraMailStatus">enabled</a>
            <a n="zimbraGalSyncTimestampFormat">yyyyMMddHHmmss'Z'</a>
            <a n="zimbraGalSyncLdapPageSize">1000</a>
            <a n="zimbraZimletDataSensitiveInMixedModeDisabled">TRUE</a>
            <a n="zimbraId">b37dSTRIPPEDecf6ac
          </domain>
        </GetAllDomainsResponse>
      </soap:Body>
    </soap:Envelope>

So the main information here (domain name) is contained within attribute "name" 
from element "<domain/>" (zimbra API is not very close to SOAP philosophy but 
that's another story…).

btw, about Simone proposal :

  What about to map attributes to a python list of dicts in this way:

  { 'tag': [ {'first_attribute': first_attribute_value}, {'second_attribute': 'second_attribute_value} ...] ...}

This proposal does not adresses the main question : how you access the nested 
levels of tags attributes ? However, it adresses the question of final 
representation (how to store the attributes of a single node).

However, XML attributes can only occur once per-tag, and 
[http://www.w3.org/TR/REC-xml/#sec-starttags their order has no importance]. So 
a dict is enough to store the attributes of a single node. list of dicts is 
overkill.

Regarding the structure ; currently, iirc, each node is a python dict ; why 
not extending python dict class overloading __getattr__ to access the 
attributes of a node ? What do you think about it ?

Original comment by joce...@crapouillou.net on 30 Sep 2013 at 9:39