Using Python's LXML I must read an XML file and print from each "basic" and "expert" tag, the name and email text from it. I've done a script that works but I don't think is the best way of doing this. Is there a better (simpler) way for getting the data of the XML without having to make 2 iterations on it?
Python so far:
from lxml import etree
myXML = "data.xml"
tree = etree.parse(myXML)
root = tree.getroot()
for node in root:
if node.tag == "basic" or node.tag == "expert":
user = [None] * 4
for i, child in enumerate(node):
if child.tag == "name":
user[0] = i
user[1] = child.text
if child.tag == "email":
user[2] = i
user[3] = child.text
print user
if user[3].startswith('_'):
# do some other things with data if email begins with _ ...
Will print:
[0, 'f.bar', 1, 'foobar@me.com']
[0, 'm.bob', 3, 'm.bob@email.com']
[0, 'm.bab', 3, 'm.bab@email.com']
XML sample:
<?xml version="1.0"?>
<users>
<id>11111</id>
<checked>True</checked>
<version>A12</mode>
<basic>
<name>f.bar</name>
<email>foobar@me.com</email>
<forename>Foo</forename>
<surname>Bar</surname>
</basic>
<expert>
<name>m.bob</name>
<forename>Mak</forename>
<surname>Bob</surname>
<email>m.bob@email.com</password>
</expert>
<expert>
<name>m.bab</name>
<forename>Mak</forename>
<surname>Bab</surname>
<email>m.bab@email.com</password>
</expert>
<guru>
<name>e.guru</name>
<forename>Nick</forename>
<email>nick@email.com</password>
<surname>Gru</surname>
</guru>
</users>