0

Estou tentando achar um elemento específico dentro de um arquivo XML em python. Para isso, uso o ElementTree.

O seguinte código:

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

Me dá como retorno a seguinte tabela:

[<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>]

Eu preciso encontrar qual o valor do elemento '{http://schemas.microsoft.com/exchange/services/2006/types}EmailAddress'

Para tal, fiz o seguinte:

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

Pelo que li, isso deveria funcionar sem problemas. Mas o que é retornado é um array vazio. Alguém tem alguma ideia do que estou fazendo errado?

EDIT: XML Completo (sem as informações):

<?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>
2
  • Pode colocar o arquivo XML completo ?
    – Felipe
    Commented 18/09/2017 às 16:25
  • Editei a pergunta e adicionei o XML. Commented 18/09/2017 às 17:45

1 Resposta 1

1

Procure pelo atributo Tag de cada elemento, que vai ter o namespace do xml e o nome da tag. Daí, basta verificar se a tag tem como substring EmailAddress e não EmailAddresses. Se isso acontecer, encontramos a TAG do email e basta extrair o texto.

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

Essa solução:

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

Não vai funcionar porque EmailAddress não é um elemento filho de root. Você precisaria colocar todos os namespaces e tags até chegar em EmailAddress para esse método funcionar.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .