In [1]:
import xml.etree.ElementTree as ET
tree = ET.parse('demo.xml')
root = tree.getroot()

In [2]:
root


Out[2]:
<Element 'bookstore' at 0x7f5d1eb1b2d0>

In [3]:
ET.dump(root)


<bookstore xmlns:ns0="uri:mynamespace" specialty="novel">
  <book style="autobiography">
    <author>
      <first-name>Joe</first-name>
      <last-name>Bob</last-name>
      <award>Trenton Literary Review Honorable Mention</award>
    </author>
    <price>12</price>
  </book>
  <book style="textbook">
    <author>
      <first-name>Mary</first-name>
      <last-name>Bob</last-name>
      <publication>Selected Short Stories of
        <first-name>Mary</first-name>
        <last-name>Bob</last-name>
      </publication>
    </author>
    <editor>
      <first-name>Britney</first-name>
      <last-name>Bob</last-name>
    </editor>
    <price>55</price>
  </book>
  <magazine frequency="monthly" style="glossy">
    <price>2.50</price>
    <subscription per="year" price="24" />
  </magazine>
  <book id="myfave" style="novel">
    <author>
      <first-name>Toni</first-name>
      <last-name>Bob</last-name>
      <degree from="Trenton U">B.A.</degree>
      <degree from="Harvard">Ph.D.</degree>
      <award>Pulitzer</award>
      <publication>Still in Trenton</publication>
      <publication>Trenton Forever</publication>
    </author>
    <price exchange="0.7" intl="Canada">6.50</price>
    <excerpt>
      <p>It was a dark and stormy night.</p>
      <p>But then all nights in Trenton seem dark and
      stormy to someone who has gone through what
      <emph>I</emph> have.</p>
      <definition-list>
        <term>Trenton</term>
        <definition>misery</definition>
      </definition-list>
    </excerpt>
  </book>
  <ns0:book price="29.50" style="leather">
    <ns0:title>Who's Who in Trenton</ns0:title>
    <ns0:author>Robert Bob</ns0:author>
  </ns0:book>
</bookstore>

In [4]:
root.findall(".")


Out[4]:
[<Element 'bookstore' at 0x7f5d1eb1b2d0>]

In [5]:
root.findall("./book")


Out[5]:
[<Element 'book' at 0x7f5d1eb1b310>,
 <Element 'book' at 0x7f5d1eb1b490>,
 <Element 'book' at 0x7f5d1eb1b810>]

In [6]:
for elemento in root.findall("./book"):
    print (elemento.tag, elemento.attrib)


('book', {'style': 'autobiography'})
('book', {'style': 'textbook'})
('book', {'style': 'novel', 'id': 'myfave'})

In [7]:
root[0][0][1].text


Out[7]:
'Bob'

In [8]:
root[3][1].text


Out[8]:
'6.50'

In [9]:
for elemento in root.findall(".//author"):
    print (elemento.tag,elemento.find('first-name').text,elemento.find('last-name').text)


('author', 'Joe', 'Bob')
('author', 'Mary', 'Bob')
('author', 'Toni', 'Bob')

In [10]:
for elemento in root.findall(".//author"):
    print (len(elemento))
    try:
        print (elemento.tag,elemento.find('./degree').text)
    except:
        print (elemento.tag,elemento.find('./first-name').text,elemento.find('last-name').text)


3
('author', 'Joe', 'Bob')
3
('author', 'Mary', 'Bob')
7
('author', 'B.A.')

In [55]:
for elemento in root.findall(".//author"):
    print (len(elemento))
    try:
        print (elemento.tag,elemento.find('./degree[@from="Harvard"]').text)
    except:
        print ("Este autor no tiene grado")


3
Este autor no tiene grado
3
Este autor no tiene grado
7
author Ph.D.

In [59]:
for elemento in root.findall(".//author"):
    print (len(elemento))
    try:
        print (elemento.tag, elemento.find('./degree').attrib)
    except:
        print ("Este autor no tiene grado")


3
Este autor no tiene grado
3
Este autor no tiene grado
7
author {'from': 'Trenton U'}

In [80]:
for elemento in root.findall('.//degree'):
    print (elemento.text, elemento.get('from'))


B.A. Trenton U
Ph.D. Harvard

In [72]:
for elemento in root.findall('.//*[@from="Harvard"]'):
    print (elemento.text)


Ph.D.

In [91]:
from lxml import etree

In [92]:
tree_lxml = etree.parse('demo.xml')
root_lxml = tree_lxml.getroot()

In [97]:
for elemento in root_lxml.findall('.//degree'):
    print (elemento.text, elemento.get('from'))


B.A. Trenton U
Ph.D. Harvard

In [ ]:


In [ ]: