Get all contents between the result tags of a SOAP response in Python
Question: I have this SOAP response :

    
    
    <?xml version="1.0" encoding="utf-8"?>
    <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>
        <GetCurrencyCodeByCurrencyNameResponse xmlns="http://www.webserviceX.NET">
            <GetCurrencyCodeByCurrencyNameResult>
                 &lt;NewDataSet /&gt;
            </GetCurrencyCodeByCurrencyNameResult>
        </GetCurrencyCodeByCurrencyNameResponse>
    </soap:Body></soap:Envelope>
    

And I use this code to get the contents of the result tag:

    
    
    import xml.etree.ElementTree as ET
    root = ET.fromstring(SoapResponse) 
    child=root[0][0][0]
    contenu= child.text
    

But when I have a response which contains other tags inside the results tag
(other children) like this SOAP response :

    
    
    <?xml version="1.0" encoding="utf-8"?>
    <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>
            <GetUserInfoResponse xmlns="http://tempuri.org/">
                <GetUserInfoResult>
                    <ErrorOccured>true</ErrorOccured>
                    <ErrorStr>System.Data.OleDb.OleDbException: Conversion failed when converting the varchar value '4CuTrO8O6Tn' to data type int.
                              at System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr)
                              at System.Data.OleDb.OleDbDataReader.NextResult()
                              at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
                              at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
                              at Service.GetUserInfo(String username, String password)
                    </ErrorStr>
                    <SqlQuery>SELECT * FROM users WHERE username=''+(select convert(int,CHAR(52)+CHAR(67)+CHAR(117)+CHAR(84)+CHAR(114)+CHAR(79)+CHAR(56)+CHAR(79)+CHAR(54)+CHAR(84)+CHAR(110)) FROM syscolumns)+'' AND password='32cc5886dc1fa8c106a02056292c4654'
                    </SqlQuery><id>-1</id><joindate>0001-01-01T00:00:00</joindate>
                </GetUserInfoResult>
            </GetUserInfoResponse>
        </soap:Body></soap:Envelope>
    

I can not get the contents between result tags with the previous code. So, how
can I get the whole contents between the result tags of a SOAP response ?


Answer: I'm not quite clear on exactly what you want, but this might do it:

    
    
    # This gets all of the text data in the indicated region
    import xml.etree.ElementTree as ET
    root = ET.fromstring(SoapResponse)
    child=root[0][0][0]
    contenu = ET.tostring(child, encoding='UTF-8', method='text').decode('UTF-8')
    

Or

    
    
    # This gets the indicated XML fragment as a string
    import xml.etree.ElementTree as ET
    root = ET.fromstring(SoapResponse)
    child=root[0][0][0]
    contenu = ET.tostring(child, encoding='UTF-8', method='xml')
    

