1

I am working with python on the creation of two types of files: EVFM and SMURF, which both follow the xml structure. One example of such files is the following:

EVFM

My approach is to use the xmltodict package with the unparse method, which transforms a dictionary into an xml-structured file.

My code is here:

mydict = OrderedDict({
        "eventfile": {
            "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "@xsi:schemaLocation": "http://esa.esoc.eventsgen_event_sample.xsd xmlns:http://esa.esoc.events",
            "@xmlns:ems":"http://esa.esoc.ems",
            "header": { "format_version":"1", "generation_time":"2022-222222",
                "validity_start": "202201028208018",
                "validity_end": "popop",
                "spacecraft": "hasbullagodsupremewewilldoit",
                "icd_version": "PROBA3-1.0",
            },
            "events": {'bot_info': {'@time': '2022-05-01T16:20:01Z', '@TM_RATE': 'HBR'},
                       'start_ul': {'@time': '2022-05-02T17:20:01Z', '@SPACECRAFT': 'OSC'},
            }
        }
    })

print(xmltodict.unparse(mydict, indent=" ", pretty=True, newl='\n'))

image version: code

To identify an attribute the symbol '@' is used, but the attributes are displayed only on the same line, not in separate lines like in the example file I provided above.

My output looks like this:

<?xml version="1.0" encoding="utf-8"?>
<eventfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://esa.esoc.eventsgen_event_sample.xsd xmlns:http://esa.esoc.events" xmlns:ems="http://esa.esoc.ems">
 <header>
  <format_version>1</format_version>
  <generation_time>2022-222222</generation_time>
  <validity_start>202201028208018</validity_start>
  <validity_end>popop</validity_end>
  <spacecraft>hasbullagodsupremewewilldoit</spacecraft>
  <icd_version>PROBA3-1.0</icd_version>
 </header>
 <events>
  <bot_info time="2022-05-01T16:20:01Z" TM_RATE="HBR"></bot_info>
  <start_ul time="2022-05-02T17:20:01Z" SPACECRAFT="OSC"></start_ul>
 </events>
</eventfile>

image version: output

I also tried with the ElementTree library, but still I couldn't find a solution.

Do you have any suggestions?

Thank you.

mzjn
  • 48,958
  • 13
  • 128
  • 248
  • Tidy has an `indent-attributes` option for this (https://api.html-tidy.org/tidy/quickref_5.8.0.html#indent-attributes). Older questions: https://stackoverflow.com/a/67130864/407651, https://stackoverflow.com/q/25447006/407651 – mzjn May 22 '23 at 11:43
  • Thank you, but I still don't know how to implement this with python, since the tidylib package doesn't seem to work on my deployment. – Luca Pizzuto May 22 '23 at 12:42
  • @LucaPizzuto why do you care about it? – balderman May 22 '23 at 18:54
  • Because I would like to have the file easily readable, and having all the attributes on one line doesn't seem optimal to me... Do you think it is not a big problem? – Luca Pizzuto May 23 '23 at 06:56

0 Answers0