简体   繁体   中英

Python parse nested xml

I have an xml file that has multiple layers of data in.

<?xml version="1.0" encoding="UTF-8"?>
<DeviceLog DevID="10503847" DocDate="2017-03-01T00:00:00" BSLogDate="2017-02-28T06:22:36">
    <Log LogTime="2017-02-27T18:33:58">
        <DevLog State="PowerOn"/>
    </Log>
    <Log LogTime="2017-02-28T08:59:03">
        <ComponentPrivateDataLog>
            <Component>1</Component>
            <DataType>1</DataType>
            <PrivateData>0301</PrivateData>
</ComponentPrivateDataLog>
    </Log>
    <Log LogTime="2017-02-28T08:59:13">
        <ComponentPrivateDataLog>
            <Component>1</Component>
            <DataType>1</DataType>
            <PrivateData>0401</PrivateData>
</ComponentPrivateDataLog>
    </Log>
    <Log LogTime="2017-02-28T10:16:44">
        <DevLog State="StandByIn"/>
    </Log>
    <Log LogTime="2017-02-28T12:29:55">
        <EndOfFileLog />
    </Log>
</DeviceLog>

In this, each Log tag is a separate entity having its own time attribute and a child node. I am using minidom to parse the data.

The following is the code:

from xml.dom import minidom
xmldoc=minidom.parse("testxml.xml")
dl=xmldoc.getElementsByTagName("DeviceLog")
for d in dl:
    dId=d.attributes["DevID"]
    dId=dId.value
    dod=d.attributes["DocDate"]
    dod=dod.value
    bsld=d.attributes["BSLogDate"]
    bsld=bsld.value

log=xmldoc.getElementsByTagName("Log")
for l in log:
    logtime = l.attributes["LogTime"]
    logtime = logtime.value 
    devLog = l.getElementsByTagName("DevLog")
    for dl in devLog:
        devEvnt = dl.attributes["State"]    
        devEvnt = devEvnt.value
print dId,dod,bsld,logtime, devEvnt

The above code prints the time and state of the StandBy (last entry) and not the first PowerOn state. I tried indexing log=xmldoc.getElementsByTagName("Log")[0] and similarly for logtime. But didn't work.

How can i parse the logs so that I get each log with time in a separate line?

If it helps you, use a special parser that reads your XML data into a pretty dictionary, which is a bit easier to deal with.

import xmltodict

myxml = """
...
"""
mydict = xmltodict.parse(myxml)
logs = mydict["DeviceLog"]["Log"]

for log in logs:
    log_time = log["@LogTime"]
    dev_log = log.get("DevLog", None)
    component_log = log.get("ComponentPrivateDataLog", None)

    if dev_log:
        print(log_time, dev_log["@State"])
    if component_log:
        print(log_time, component_log["Component"], component_log["PrivateData"])

Example of such a parser: xmltodict .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM