Reputation: 595
How can I get the phone number (00 40 85 55) from the Number
node?
import xml.etree.ElementTree as ET
tree = ET.parse('/home/user/foo.contact')
root = tree.getroot()
print(root.findall("./PhoneNumberCollection/Number"))
cat foo.contact
<?xml version="1.0" encoding="UTF-8"?>
<c:contact c:Version="1" xmlns:c="http://schemas.microsoft.com/Contact" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:MSP2P="http://schemas.microsoft.com/Contact/Extended/MSP2P">
<c:CreationDate>2013-10-23T07:24:09Z</c:CreationDate><c:Extended xsi:nil="true"/>
<c:ContactIDCollection><c:ContactID c:ElementID="47834c1a-b1b1-41ce-8747-a0729b6772c5"><c:Value>6398d809-02c3-46f3-a924-f26e5bee0de1</c:Value></c:ContactID></c:ContactIDCollection><c:NameCollection><c:Name c:ElementID="9d0727c6-6263-44f5-b204-0a685a867181"><c:FormattedName>Alexander Foo</c:FormattedName><c:FamilyName>Foo</c:FamilyName><c:GivenName>Alexander</c:GivenName></c:Name></c:NameCollection><c:PhoneNumberCollection><c:PhoneNumber c:ElementID="4a253260-b8f9-4dbf-9f0f-f5639d7a8d5a"><c:Number c:Version="2" c:ModificationDate="2014-05-12T10:23:53Z">00 40 85 55</c:Number><c:LabelCollection><c:Label>Cellular</c:Label></c:LabelCollection></c:PhoneNumber></c:PhoneNumberCollection><c:PhotoCollection><c:Photo c:ElementID="346a0417-0378-4555-a22e-1757f0522119"><c:LabelCollection><c:Label>UserTile</c:Label></c:LabelCollection></c:Photo></c:PhotoCollection><c:EmailAddressCollection c:Version="1" c:ModificationDate="2014-05-12T10:23:53Z"><c:EmailAddress c:ElementID="a8f077b3-dad7-4941-bde2-50acf4433f3e" c:Version="1" c:ModificationDate="2014-05-12T10:23:53Z"><c:Address c:Version="1" c:ModificationDate="2014-05-12T10:23:53Z">[email protected]</c:Address><c:LabelCollection><c:Label c:Version="1" c:ModificationDate="2014-05-12T10:23:53Z">Preferred</c:Label></c:LabelCollection></c:EmailAddress></c:EmailAddressCollection></c:contact>
I wasn't able to properly format the xml, please bear with me here
I read[0] and tried to no avail
[0] https://docs.python.org/3/library/xml.etree.elementtree.html#finding-interesting-elements
Upvotes: 0
Views: 81
Reputation: 23825
The below seems to work. The idea is to use the 'namespace' as part of the findall
argument.
import xml.etree.ElementTree as ET
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<c:contact xmlns:c="http://schemas.microsoft.com/Contact" xmlns:MSP2P="http://schemas.microsoft.com/Contact/Extended/MSP2P" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" c:Version="1">
<c:CreationDate>2013-10-23T07:24:09Z</c:CreationDate>
<c:Extended xsi:nil="true" />
<c:ContactIDCollection>
<c:ContactID c:ElementID="47834c1a-b1b1-41ce-8747-a0729b6772c5">
<c:Value>6398d809-02c3-46f3-a924-f26e5bee0de1</c:Value>
</c:ContactID>
</c:ContactIDCollection>
<c:NameCollection>
<c:Name c:ElementID="9d0727c6-6263-44f5-b204-0a685a867181">
<c:FormattedName>Alexander Foo</c:FormattedName>
<c:FamilyName>Foo</c:FamilyName>
<c:GivenName>Alexander</c:GivenName>
</c:Name>
</c:NameCollection>
<c:PhoneNumberCollection>
<c:PhoneNumber c:ElementID="4a253260-b8f9-4dbf-9f0f-f5639d7a8d5a">
<c:Number c:Version="2" c:ModificationDate="2014-05-12T10:23:53Z">00 40 85 55</c:Number>
<c:LabelCollection>
<c:Label>Cellular</c:Label>
</c:LabelCollection>
</c:PhoneNumber>
</c:PhoneNumberCollection>
<c:PhotoCollection>
<c:Photo c:ElementID="346a0417-0378-4555-a22e-1757f0522119">
<c:LabelCollection>
<c:Label>UserTile</c:Label>
</c:LabelCollection>
</c:Photo>
</c:PhotoCollection>
<c:EmailAddressCollection c:Version="1" c:ModificationDate="2014-05-12T10:23:53Z">
<c:EmailAddress c:ElementID="a8f077b3-dad7-4941-bde2-50acf4433f3e" c:Version="1" c:ModificationDate="2014-05-12T10:23:53Z">
<c:Address c:Version="1" c:ModificationDate="2014-05-12T10:23:53Z">[email protected]</c:Address>
<c:LabelCollection>
<c:Label c:Version="1" c:ModificationDate="2014-05-12T10:23:53Z">Preferred</c:Label>
</c:LabelCollection>
</c:EmailAddress>
</c:EmailAddressCollection>
</c:contact>'''
root = ET.fromstring(xml)
numbers = root.findall('.//{http://schemas.microsoft.com/Contact}Number')
for number in numbers:
print(number.text)
output
00 40 85 55
Upvotes: 1