I am trying to create a simple XML file using python3 with the following output.
<products>
<product>
<id>1</id>
<name>Apple</name>
</product>
<product>
<id>2</id>
<name>Banana</name>
</product>
</products>
I went through the xml.etree.ElementTree however i couldnot find the exact way to complete as above
I could achieve something like this
<products>
<product>
<id>1</id>
<name>Apple</name>
<id>2</id>
<name>Banana</name>
</product>
</products>
The following is my code i used:
import xml.etree.cElementTree as ET
root = ET.Element("products")
doc = ET.SubElement(root, "product")
ET.SubElement(doc, "id").text = "some value1"
ET.SubElement(doc, "name").text = "some vlaue1"
ET.SubElement(doc, "id").text = "some value2"
ET.SubElement(doc, "name").text = "some vlaue2"
tree = ET.ElementTree(root)
tree.write("filename.xml")
I want to create different product sub element under the root products. Any suggestion on how to accomplish this will be great.