Problem parsing SOAP envelope with ElementTree

Discussion in 'Python' started by Zvi, May 7, 2008.

  1. Zvi

    Zvi Guest

    Hi All,

    Can someone tell me why id the following not working?
    I have a soap response envelope, for test purpose it's just a string
    and I create ElementTree from it.
    Then I try to find Response tag, but I get None.


    data = """<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/
    soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body> <Get2Response xmlns="http://
    tempuri.org/">
    <Get2Result><![CDATA[<?xml version='1.0'
    encoding='UTF-8'?> <Response> <Entity Name='Accounts'
    Current='00300571B42F1DEC8E9B6CDF19A59950'> <Instance
    Id='00300571B42F1DEC8E9B6CDF19A59950'> <Field
    Name='Status' Value='2'/> <Field Name='Id' Value='MC4670'/> <Field
    Name='Name' Value='ACDC Industries Inc'/>
    <Field Name='City' Value='Milwaukee'/> <Field
    Name='MainContact' Value=''/> <Field Name='CreatedOn'
    Value='20070723051316.0000000 '/> </Instance> </Entity> </
    Response>]]></Get2Result>
    </Get2Response></soap:Body></soap:Envelope>"""

    ElementTree.XMLTreeBuilder = SimpleXMLTreeBuilder.TreeBuilder
    dom=ElementTree.fromstring(data)

    if dom == None:
    return "Empty Dom"

    response = dom.find('{http://schemas.xmlsoap.org/soap/
    envelope/}Response')
    if response == None:
    return "Empty Response"

    if I try to use
    response = dom.find('{http://tempuri.org/}Response') this doesn't work
    either.

    What am I doing wrong?
     
    Zvi, May 7, 2008
    #1
    1. Advertising

  2. Zvi wrote:
    > Hi All,
    >
    > Can someone tell me why id the following not working?


    snip not working code

    >
    > What am I doing wrong?


    Here is working code.

    8<-----------------------------
    from xml.etree import ElementTree as ET

    data = """<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
    <Get2Response xmlns="http://tempuri.org/">
    <Get2Result><![CDATA[<?xml version='1.0' encoding='UTF-8'?>
    <Response>
    <Entity Name='Accounts' Current='00300571B42F1DEC8E9B6CDF19A59950'>
    <Instance Id='00300571B42F1DEC8E9B6CDF19A59950'>
    <Field Name='Status' Value='2'/>
    <Field Name='Id' Value='MC4670'/>
    <Field Name='Name' Value='ACDC Industries Inc'/>
    <Field Name='City' Value='Milwaukee'/>
    <Field Name='MainContact' Value=''/>
    <Field Name='CreatedOn' Value='20070723051316.0000000 '/>
    </Instance>
    </Entity>
    </Response>]]>
    </Get2Result>
    </Get2Response>
    </soap:Body>
    </soap:Envelope>"""

    env = ET.fromstring(data)
    result = env.find('*//{http://tempuri.org/}Get2Result')
    response = ET.fromstring(result.text)
    for elm in response.getiterator():
    print elm
    8<-----------------------------------------

    In the future please paste complete examples. It helps me to help you.
    They were two things that I found to be wrong:
    - searching using wrong path. You missed *// in front of the tag
    - parsing only once. There are two xml sources here.
    The first parse got you the representation of the Envelope.
    You have to parse the Get2Result payload to get at the
    interesting part

    Waldemar
     
    Waldemar Osuch, May 7, 2008
    #2
    1. Advertising

  3. Zvi

    Zvi Guest

    On May 7, 1:12 am, Waldemar Osuch <> wrote:
    Thanks a lot :)
    P.S. It was a complete code, I didn't write anything else.
     
    Zvi, May 7, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    0
    Views:
    548
  2. cxc
    Replies:
    0
    Views:
    6,549
  3. Replies:
    0
    Views:
    486
  4. Kee Nethery
    Replies:
    12
    Views:
    2,167
    Stefan Behnel
    Jun 27, 2009
  5. droesler
    Replies:
    2
    Views:
    563
    droesler
    Aug 31, 2010
Loading...

Share This Page