Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I parse this xml string in python?

My XML string is -

xmlData = """<SMSResponse xmlns="http://example.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
             <Cancelled>false</Cancelled>
             <MessageID>00000000-0000-0000-0000-000000000000</MessageID>  
             <Queued>false</Queued>
             <SMSError>NoError</SMSError>
             <SMSIncomingMessages i:nil="true"/>
             <Sent>false</Sent>
             <SentDateTime>0001-01-01T00:00:00</SentDateTime>
             </SMSResponse>"""

I am trying to parse and get the values of tags - Cancelled, MessageId, SMSError, etc. I am using python's Elementtree library. So far, I have tried things like -

root = ET.fromstring(xmlData)
print root.find('Sent')  // gives None
for child in root:
    print chil.find('MessageId') // also gives None

Although, I am able to print the tags with -

for child in root:
    print child.tag
    //child.tag for the tag Cancelled is - {http://example.com}Cancelled

and their respective values with -

for child in root:
    print child.text

How do I get something like -

print child.Queued // will print false

Like in PHP we can access them with the root -

$xml = simplexml_load_string($data);
$status = $xml->SMSError;
like image 277
Hussain Avatar asked Jan 04 '13 09:01

Hussain


Video Answer


2 Answers

Your document has a namespace on it, you need to include the namespace when searching:

root = ET.fromstring(xmlData)
print root.find('{http://example.com}Sent',)
print root.find('{http://example.com}MessageID')

output:

<Element '{http://example.com}Sent' at 0x1043e0690>
<Element '{http://example.com}MessageID' at 0x1043e0350>

The find() and findall() methods also take a namespace map; you can search for a arbitrary prefix, and the prefix will be looked up in that map, to save typing:

nsmap = {'n': 'http://example.com'}
print root.find('n:Sent', namespaces=nsmap)
print root.find('n:MessageID', namespaces=nsmap)
like image 116
Martijn Pieters Avatar answered Oct 14 '22 13:10

Martijn Pieters


If you're set on Python standard XML libraries, you could use something like this:

root = ET.fromstring(xmlData)
namespace = 'http://example.com'

def query(tree, nodename):
    return tree.find('{{{ex}}}{nodename}'.format(ex=namespace, nodename=nodename))

queued = query(root, 'Queued')
print queued.text
like image 37
tuomur Avatar answered Oct 14 '22 13:10

tuomur