Findall using Elementtree does not return expected result

Asked

Viewed 77 times

0

I am trying to find a specific element within a python XML file. I use Elementtree for this.

The following code:

    root = ET.fromstring(response.content.decode('utf-8'))
    print(root.findall('.//*'))

Give me back the following table:

[<Element '{http://schemas.xmlsoap.org/soap/envelope/}Header' at 0x000002034CB3B908>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}ServerVersionInfo' at 0x000002034CB16BD8>, 
<Element '{http://schemas.xmlsoap.org/soap/envelope/}Body' at 0x000002034CB16CC8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/messages}ResolveNamesResponse' at 0x000002034CB16E08>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/messages}ResponseMessages' at 0x000002034CB169F8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/messages}ResolveNamesResponseMessage' at 0x000002034CB16AE8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/messages}ResponseCode' at 0x000002034CB59548>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/messages}ResolutionSet' at 0x000002034CB59598>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Resolution' at 0x000002034CB595E8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Mailbox' at 0x000002034CB59638>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Name' at 0x000002034CB59688>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}EmailAddress' at 0x000002034CB596D8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}RoutingType' at 0x000002034CB59728>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}MailboxType' at 0x000002034CB59818>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Contact' at 0x000002034CB59868>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Culture' at 0x000002034CB598B8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}DisplayName' at 0x000002034CB59908>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}GivenName' at 0x000002034CB59958>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Initials' at 0x000002034CB599A8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}CompanyName' at 0x000002034CB599F8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}EmailAddresses' at 0x000002034CB59A48>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59A98>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59AE8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59B38>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}PhysicalAddresses' at 0x000002034CB59B88>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59BD8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Street' at 0x000002034CB59C28>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}City' at 0x000002034CB59C78>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}State' at 0x000002034CB59CC8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}CountryOrRegion' at 0x000002034CB59D18>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}PostalCode' at 0x000002034CB59D68>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}PhoneNumbers' at 0x000002034CB59DB8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59E08>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59E58>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59EA8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59EF8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59F48>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Entry' at 0x000002034CB59F98>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}AssistantName' at 0x000002034CBA2048>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}ContactSource' at 0x000002034CBA2098>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Department' at 0x000002034CBA20E8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}JobTitle' at 0x000002034CBA2138>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Manager' at 0x000002034CBA2188>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}OfficeLocation' at 0x000002034CBA21D8>, 
<Element '{http://schemas.microsoft.com/exchange/services/2006/types}Surname' at 0x000002034CBA2228>]

I need to find the value of the element '{http://schemas.microsoft.com/exchange/services/2006/types}EmailAddress'

To that end, I’ve done the following:

teste = root.findall('{http://schemas.microsoft.com/exchange/services/2006/types}EmailAddress')
print(teste)

From what I read, this should work smoothly. But what is returned is an empty array. Does anyone have any idea what I’m doing wrong?

EDIT: XML Complete (without the information):

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header>
      <h:ServerVersionInfo xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" MajorVersion="15" MinorVersion="1" MajorBuildNumber="669" MinorBuildNumber="32" />
   </s:Header>
   <s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <m:ResolveNamesResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
         <m:ResponseMessages>
            <m:ResolveNamesResponseMessage ResponseClass="Success">
               <m:ResponseCode>***</m:ResponseCode>
               <m:ResolutionSet TotalItemsInView="1" IncludesLastItemInRange="true">
                  <t:Resolution>
                     <t:Mailbox>
                        <t:Name>***</t:Name>
                        <t:EmailAddress>***</t:EmailAddress>
                        <t:RoutingType>***</t:RoutingType>
                        <t:MailboxType>***</t:MailboxType>
                     </t:Mailbox>
                     <t:Contact>
                        <t:Culture>***</t:Culture>
                        <t:DisplayName>***</t:DisplayName>
                        <t:GivenName>***</t:GivenName>
                        <t:Initials />
                        <t:CompanyName>***</t:CompanyName>
                        <t:EmailAddresses>
                           <t:Entry Key="EmailAddress1">***</t:Entry>
                           <t:Entry Key="EmailAddress2">***</t:Entry>
                           <t:Entry Key="EmailAddress3">***</t:Entry>
                        </t:EmailAddresses>
                        <t:PhysicalAddresses>
                           <t:Entry Key="Business">
                              <t:Street>***</t:Street>
                              <t:City>***</t:City>
                              <t:State />
                              <t:CountryOrRegion>***</t:CountryOrRegion>
                              <t:PostalCode>***</t:PostalCode>
                           </t:Entry>
                        </t:PhysicalAddresses>
                        <t:PhoneNumbers>
                           <t:Entry Key="AssistantPhone" />
                           <t:Entry Key="BusinessFax" />
                           <t:Entry Key="BusinessPhone">***</t:Entry>
                           <t:Entry Key="HomePhone" />
                           <t:Entry Key="MobilePhone">***</t:Entry>
                           <t:Entry Key="Pager" />
                        </t:PhoneNumbers>
                        <t:AssistantName />
                        <t:ContactSource>***</t:ContactSource>
                        <t:Department>***</t:Department>
                        <t:JobTitle />
                        <t:Manager>***</t:Manager>
                        <t:OfficeLocation>***</t:OfficeLocation>
                        <t:Surname>***</t:Surname>
                     </t:Contact>
                  </t:Resolution>
               </m:ResolutionSet>
            </m:ResolveNamesResponseMessage>
         </m:ResponseMessages>
      </m:ResolveNamesResponse>
   </s:Body>
</s:Envelope>
  • You can put the full XML file ?

  • I edited the question and added XML.

1 answer

1


Look for the tag attribute of each element, which will have the xml namespace and tag name. Then, just check if the tag is substring Emailaddress and not Emailaddresses. If this happens, we find the email TAG and just extract the text.

for el in root.findall('.//*'):
        if el.tag.find('EmailAddress') != -1 and el.tag.find('EmailAddresses') == -1 :
            print(el.text)

That solution:

root.findall('{http://schemas.microsoft.com/exchange/services/2006/types}EmailAddress')

It won’t work because Emailaddress is not an element child of root. You’d need to put all the namespaces and tags until you get into Emailaddress for this method to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.