Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 _ ...
- [0, 'f.bar', 1, 'foobar@me.com']
- [0, 'm.bob', 3, 'm.bob@email.com']
- [0, 'm.bab', 3, 'm.bab@email.com']
- <?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>
Add Comment
Please, Sign In to add comment