Seite 1 von 1

XML Daten auslesen

Verfasst: Mittwoch 10. Dezember 2008, 12:19
von DjFresH
hallo,

brauch da mal n bissl hilfe beim xml auslesen...
und zwar habe ich eine xml mit folgender struktur:

Code: Alles auswählen

<root>
 <Processors>
  <ProcessorType>TXCPU</ProcessorType> 
   <DeviceFile>
    <FileName>NpTx</FileName> 
    <Version>1_a</Version> 
   </DeviceFile>
 </Processors>
 <CommunicationMedia>
  <CommunicationMediumType>CANA</CommunicationMediumType> 
   <DeviceFile>
    <FileName>NpCan</FileName> 
    <Version>1</Version> 
   </DeviceFile>
   <DriverFile>
    <FileName>CanPci405</FileName> 
    <Version>1</Version> 
   </DriverFile>
 </CommunicationMedia>
</root>
und ich habe folgenden Script dazu:

Code: Alles auswählen

import xml.etree.ElementTree as et

files = file("project.xml", "r") 
etree = et.parse(files) 
files.close() 
root_tag = etree.getroot() 

for proc in root_tag.findall("Processors"):
    for device in proc.findall("DeviceFile"):
        print device.find("FileName").text
        print device.find("Version").text        #Bis hier gehts!!!

for commed in root_tag.findall("CommunicationMedia"):
    for comtype in commed.getchildren("CommunicationMediumType"):
        for devf in comtype.findall("DeviceFile"):
            print devf.find("FileName").text
            print devf.find("Version").text
        for driver in comtype.findall("DriverFile"):
            print driver.find("FileName").text
            print driver.find("Version").text
Warum spukt der mir bei CommunicationMedia nichts aus?

Verfasst: Mittwoch 10. Dezember 2008, 12:35
von helduel
Moin,

weil "DeviceFile" kein Kind von "CommunicationMediumType" ist.

Gruß,
Manuel

Verfasst: Mittwoch 10. Dezember 2008, 12:52
von DjFresH
ah ja danke für den hint....

neuer code:

Code: Alles auswählen

for commed in root_tag.findall("CommunicationMedia"):
    print commed.find("CommunicationMediumType").text
    for devf in commed.findall("DeviceFile"):
        print devf.find("FileName").text
        print devf.find("Version").text
    for driver in commed.findall("DriverFile"):
        print driver.find("FileName").text
        print driver.find("Version").text
...jetzt gehts...

..noch ne kleine frage:
Welche bib oder wie heisst die datei die zu xml.etree.ElementTree gehört??

Verfasst: Mittwoch 10. Dezember 2008, 13:09
von helduel

Code: Alles auswählen

from xml.etree import ElementTree
ElementTree.__file__