Domanda

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.

È stato utile?

Soluzione

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

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

Altri suggerimenti

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top