XML Daten auslesen

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
DjFresH
User
Beiträge: 36
Registriert: Donnerstag 20. November 2008, 08:28

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?
Benutzeravatar
helduel
User
Beiträge: 300
Registriert: Montag 23. Juli 2007, 14:05
Wohnort: Laupheim

Moin,

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

Gruß,
Manuel
DjFresH
User
Beiträge: 36
Registriert: Donnerstag 20. November 2008, 08:28

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??
Benutzeravatar
helduel
User
Beiträge: 300
Registriert: Montag 23. Juli 2007, 14:05
Wohnort: Laupheim

Code: Alles auswählen

from xml.etree import ElementTree
ElementTree.__file__
Antworten