What will be the content of the cars.xml file when you run the following code? import xml.etree.ElementTree as ET root = ET.Element('data') car_1 = ET.SubElement(root, 'car', {'brand': 'Audi'}) car_2 = ET.SubElement(root, 'car', {'brand': 'Volkswagen'}) tree = ET.ElementTree(root) tree.write('cars.xml', 'UTF-8', True)
正解:C
The correct answer is C. The code creates an XML root element named data and then adds two child elements named car. Each car element receives a brand attribute, one with the value Audi and the other with the value Volkswagen. The statement tree.write('cars.xml', 'UTF-8', True) writes the XML document using UTF-8 encoding and explicitly enables the XML declaration through the third argument. Because the XML declaration is enabled, the output begins with < ?xml version='1.0' encoding='UTF-8'? > . The generated XML body then contains the data element with two self-closing car child elements. Options without the encoding declaration are therefore incomplete.