Data formats: XML, INI, TOML and pickle · 9 / 15 Exercise Medium

Generate an XML document

Objective

To create XML you do not concatenate text: you build the tree with Element and SubElement, and tostring takes care of escaping special characters such as < and &.

Instructions

  • The first line has the number n of entries; each one is a line name value, where the value is the rest of the line.
  • Build a root element data with one entry child per entry (attribute name and the value as text) and show the XML with ET.tostring(root, encoding="unicode").
Input
2
a 1
b hello world
Output
<data><entry name="a">1</entry><entry name="b">hello world</entry></data>

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise