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

Total of an order in XML

Objective

XML organizes data in a tree of elements with tags, attributes and text. The xml.etree.ElementTree module turns them into objects you can walk.

Instructions

  • The input is a line with an XML document. It contains item elements (at any level) with the attribute price (decimal) and, optionally, qty (integer, 1 if missing).
  • Show the total of price * qty with 2 decimals. Use fromstring and iter.
Input
<shop><item price="2.5" qty="2"/><item price="1.25"/></shop>
Output
6.25

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

Start exercise