1

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.

Sijan Shrestha
  • 2,136
  • 7
  • 26
  • 52

3 Answers3

5

The first argument of SubElement is the tag to which you append the newly created element. In your example all id and name elements are appended to the first product you created - doc.

Try creating a second one the same way and appending to that one, i.e. add

doc2 = ET.SubElement(root, "product")

and switch doc2 for doc in your second set of product details.

Oliver
  • 351
  • 2
  • 6
1

if position of insertion is needed use insert

from xml.etree.ElementTree import Element,SubElement,Comment,tostring
from xml.dom.minidom import parseString

root = Element("products")
first = Element("product")
SubElement(first,"id").text="1"
SubElement(first,"name").text="Apple"

second = Element("product")
SubElement(second,"id").text="2"
SubElement(second,"name").text="Banana"
root.insert(1,first)
root.insert(2,second)
print(parseString(tostring(root,'utf-8')).toprettyxml(indent=" "))
Abhishek D K
  • 2,257
  • 20
  • 28
1

I know its been answered long back, however, my solution might help people who is looking for complete solution.

Below script reads an xml and add on 2 new tags with text.

import sys
import xml.etree.ElementTree as ET


# Tags to be added
stringsToAddAsText = ('new1', 'new2') 

# Element name
tag = 'InstallationInclude' 

# Sub element name
subTag = 'IS_NEW'

# getting the File name as runtime argument
fileName = sys.argv[1]

# Passing the path of the xml document to enable the parsing process
tree = ET.parse(fileName)
 
# getting the parent tag of the xml document
root = tree.getroot()
 
# printing the root (parent) tag
# of the xml document, along with
# its memory location
print(root)


# add new elements 
child = ET.Element(tag)
schild = ET.SubElement(child, subTag)
schild.text = stringsToAddAsText[0]
child1 = ET.Element(tag)
schild1 = ET.SubElement(child1, subTag)
schild1.text = stringsToAddAsText[1]                       
root.append(child)
root.append(child1)
       
ET.dump(root)
ET.indent(tree, space="\t", level=0)

# getting the output file name as a runtime argument and writes the updated file
tree.write(sys.argv[2], encoding="utf-8",xml_declaration=True)

Output:

<?xml version='1.0' encoding='utf-8'?>
<Installation InstallationVersion="1.0.0">  
    <InstallationInclude>
        <IS_NEW>new1</IS_NEW>
    </InstallationInclude>
    <InstallationInclude>
        <IS_NEW>new2</IS_NEW>
    </InstallationInclude>
</Installation>