Skip to content

Instantly share code, notes, and snippets.

@matagus
Created July 4, 2011 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matagus/1062757 to your computer and use it in GitHub Desktop.
Save matagus/1062757 to your computer and use it in GitHub Desktop.
processing xml with namespaces with python
<?xml version="1.0" encoding="UTF-8"?>
<hen:logs xmlns:hen="http://www.site.de/schemas/logs/1.0">
<!--<hen:vhost>
<hen:uuid></hen:uuid>
</hen:vhost>-->
<hen:vhost>www.site1.de
<hen:uuid>c5328180-e52b-4af5-bf6e-7a9e7c1754e2</hen:uuid>
</hen:vhost>
<hen:vhost>www.site2.de
<hen:uuid>b7b968db-27c7-4430-8346-b2c6e2de8065</hen:uuid>
</hen:vhost>
<hen:vhost>www.site3.de
<hen:uuid>243564bc-0f86-463a-9188-668ea6025edf</hen:uuid>
</hen:vhost>
</hen:logs>
#!/usr/bin/env python
from lxml.etree import cElementTree
tree = lxml.etree.parse("dict.xml")
NS = '{http://www.site.de/schemas/logs/1.0}'
NS_vhost = tree.findall('//{NS}vhost'.format(NS=NS))
NS_uuid = tree.findall('//{NS}uuid'.format(NS=NS))
d = {}
for vhost in NS_vhost:
d1 = vhost.text
print d1
for uuid in NS_uuid:
d2 = uuid.text
print d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment