Reputation: 1930
I have XML like this: (this is the soup variable)
<?xml version="1.0" encoding="utf-8"?>
<results>
<status code="ok">
</status>
<principal-list>
<principal account-id="1013353221" has-children="false" is-hidden="false" is-primary="false" principal-id="1013353225" training-group-id="" type="user">
<name>
First Last
</name>
<login>
first.last
</login>
<email>
[email protected]
</email>
<display-uid>
first.last
</display-uid>
</principal>
<principal account-id="1013353221" has-children="false" is-hidden="false" is-primary="false" principal-id="1115269931" training-group-id="" type="user">
<name>
First Last
</name>
<login>
first.last
</login>
<email>
[email protected]
</email>
<display-uid>
first.last
</display-uid>
</principal>
</principal-list>
</results>
I've tried this code:
loginlist = [el.string for el in soup.findAll('login')]
for entry in loginlist:
print entry
It returns:
first.last
for each of the users.
I want to also find the principal-id
for the nested login
My output would look like:
`'first.last', '1013353225'`
`'first.last', '1115269931'`
Upvotes: 0
Views: 87
Reputation: 1122152
Look for the <principal>
elements instead:
for principal in soup.findAll('principal'):
login = principal.find('login')
if login is None:
continue
print principal['principal-id'], login.string
Upvotes: 2