Skip to content

Generating XML Datlog files using Python

Mirco Dilly edited this page May 21, 2021 · 1 revision

Generating XML-Datlog files using Python

Method 1: Using xmltodict

The simulation data is encoded in dictionaries {'KeyX':{'Value':valX,'Unit':unitX}}:

import xmltodict

simdata = {'Logfile':{'Rate':{'Value':10000,'Unit':'kbps'}, 'YPSNR':{'Value':32.5,'Unit':'dB'}, 'EncTime':{'Value':12345,'Unit':'s'}}}

Now the simulation data can be encoded into some xml-datlog file by using the xmltodict-module:

xml1 = xmltodict.unparse(simdata,pretty=True)

This yields the following string, which can then be stored to some xml-file and parsed with RDPlot:

<?xml version="1.0" encoding="utf-8"?>
<Logfile>
    <Rate>
        <Value>10000</Value>
        <Unit>kbps</Unit>
    </Rate>
    <YPSNR>
        <Value>32.5</Value>
        <Unit>dB</Unit>
    </YPSNR>
    <EncTime>
        <Value>12345</Value>
        <Unit>s</Unit>
    </EncTime>
</Logfile>

Method 2: Using dicttoxml

The simulation data is again encoded in dictionaries. However, this time the dicttoxml-module is used to create the xml-formatted string:

import dicttoxml

xml2 = dicttoxml.dicttoxml(simdata, attr_type=False)

The xml-file will look like this:

<?xml version="1.0" ?>
<root>
    <Logfile>
        <Rate>
            <Value>10000</Value>
            <Unit>kbps</Unit>
        </Rate>
        <YPSNR>
            <Value>32.5</Value>
            <Unit>dB</Unit>
        </YPSNR>
        <EncTime>
            <Value>12345</Value>
            <Unit>s</Unit>
        </EncTime>
    </Logfile>
</root>

However, this module does not support reverse-parsing from the xml-formatted file to the dictionary structure.