Frage

xml =

<company>Mcd</company>        
<Author>Dr.D</Author> 

I want to fetch Mcd and Dr.D.
My try

import xml.etree.ElementTree as et
e = et.parse(xml)
root = e.getroot()
for node in root.getiterator("company"):
   print node.tag

Hopping for a generous help.

War es hilfreich?

Lösung

Simply find the one tag that matches, then take the .text attribute:

company = root.find('.//company').text
author = root.find('.//Author').text

Andere Tipps

Try this.

   from xml.etree import ElementTree as ET
    xmlFile = ET.iterparse(open('some_file.xml','r'))

    for tag, value in xmlFile:
        print value.text
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top