mateo
mateo

Reputation: 37

Python XML SOAP parsing

I have trying to get list of all ROW elements from SOAP xml by ET.findall('*/ROW') method (xml.etree.ElementTree as ET), but without result. The xml look like:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><SOAP-CHK:Success xmlns:SOAP-CHK = "http://soaptest1/soaptest/" xmlns="urn:candle-soap:attributes"><TABLE name="O4SRV.TSITSTSH">
<OBJECT>Status_History</OBJECT>
<DATA>
    <ROW>
        <Global_Timestamp>tttt01</Global_Timestamp>
        <Situation_Name>nnn01</Situation_Name>
        <Status>Raised</Status>
        <Atomize></Atomize>
    </ROW>
    <ROW>
        <Global_Timestamp>tttt02</Global_Timestamp>
        <Situation_Name>nnn02</Situation_Name>
        <Status>Reset</Status>
        <Atomize></Atomize>
    </ROW>
    <ROW>
        <Global_Timestamp>tttt03</Global_Timestamp>
        <Situation_Name>nnn03</Situation_Name>
        <Status>Raised</Status>
        <Atomize></Atomize>
    </ROW>
</DATA>
</TABLE>
</SOAP-CHK:Success></SOAP-ENV:Body></SOAP-ENV:Envelope>

When I remove all SOAP tags from this xml, everything its ok, getting list of ROWs. How I must modify string in findall method to get list of ROW elements without remove SOAP tags?

Trying also

'*/SOAP-ENV:Envelope[@SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"]/SOAP-ENV:Body/SOAP-CHK:Success[@xmlns="urn:candle-soap:attributes"]/TABLE[@name="O4SRV.TSITSTSH"]/DATA//ROW' 

and other combinations, but without results, getting error:

SyntaxError: prefix 'SOAP-ENV' not found in prefix map

or

SyntaxError: prefix 'SOAP-ENV' not found in prefix map

Upvotes: 1

Views: 1595

Answers (1)

Ali
Ali

Reputation: 1357

I basically extracted the text value of all tags under all <ROW> tags.

Is that what you're looking for?

Code:

from bs4 import BeautifulSoup

xml = '''
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><SOAP-CHK:Success xmlns:SOAP-CHK = "http://soaptest1/soaptest/" xmlns="urn:candle-soap:attributes"><TABLE name="O4SRV.TSITSTSH">
<OBJECT>Status_History</OBJECT>
<DATA>
    <ROW>
        <Global_Timestamp>tttt01</Global_Timestamp>
        <Situation_Name>nnn01</Situation_Name>
        <Status>Raised</Status>
        <Atomize></Atomize>
    </ROW>
    <ROW>
        <Global_Timestamp>tttt02</Global_Timestamp>
        <Situation_Name>nnn02</Situation_Name>
        <Status>Reset</Status>
        <Atomize></Atomize>
    </ROW>
    <ROW>
        <Global_Timestamp>tttt03</Global_Timestamp>
        <Situation_Name>nnn03</Situation_Name>
        <Status>Raised</Status>
        <Atomize></Atomize>
    </ROW>
</DATA>
</TABLE>
</SOAP-CHK:Success></SOAP-ENV:Body></SOAP-ENV:Envelope>
'''

soup = BeautifulSoup(xml, 'xml')
for i in soup.find_all('ROW'):
    print(i.text)

Output:

tttt01
nnn01
Raised



tttt02
nnn02
Reset



tttt03
nnn03
Raised

Upvotes: 2

Related Questions