You may now set up one or more child elements to be added under root element. Each child may have one or more subelements. Add them using Subelement() function and define it's text attribute.,XML is a tree like hierarchical data format. The 'ElementTree' in this module treats the whole XML document as a tree. the 'Element' class represents a single node in this tree. Reading and writing operations on XML files are done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level.,The tree is a hierarchical structure of elements starting with root followed by other elements. Each element is created by using Element() function of this module.,Let us now read back the 'myfile.xml' created in above example. For this purpose following functions in ElementTree module will be used
The tree is a hierarchical structure of elements starting with root followed by other elements. Each element is created by using Element() function of this module.
import xml.etree.ElementTree as et
e = et.Element('name')
Each element is characterized by a tag and attrib attribute which is a dict object. For tree's starting element, attrib is an empty dictionary
>>> root = xml.Element('employees') >>>
root.tag 'emploees' >>>
root.attrib {}
You may now set up one or more child elements to be added under root element. Each child may have one or more subelements. Add them using Subelement() function and define it's text attribute.
child = xml.Element("employee")
nm = xml.SubElement(child, "name")
nm.text = student.get('name')
age = xml.SubElement(child, "salary")
age.text = str(student.get('salary'))
After adding required number of child elements, construct a tree object by elementTree() function
tree = et.ElementTree(root)
The entire tree structure is written to a binary file by tree object's write() function
f = open('employees.xml', "wb")
tree.write(f)
ElementTree provides a simple way to build XML documents and write them to files. The ElementTree.write() method serves this purpose.,fromstring() parses XML from a string directly into an Element, which is the root element of the parsed tree. Other parsing functions may create an ElementTree. Check the documentation to be sure.,Element class. This class defines the Element interface, and provides a reference implementation of this interface.,Replaces the root element for this tree. This discards the current contents of the tree, and replaces it with the given element. Use with care. element is an element instance.
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N" />
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W" />
<neighbor name="Colombia" direction="E" />
</country>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
root = ET.fromstring(country_data_as_string)
>>> root.tag 'data' >>>
root.attrib {}
>>>
for child in root:
...print(child.tag, child.attrib)
...
country {
'name': 'Liechtenstein'
}
country {
'name': 'Singapore'
}
country {
'name': 'Panama'
}
>>> root[0][1].text '2008'
You opened the file for appending, which adds data to the end. Open the file for writing instead, using the w
mode. Better still, just use the .write()
method on the ElementTree object:
tree = xml.parse("/tmp/" + executionID + ".xml")
xmlRoot = tree.getroot()
child = xml.Element("NewNode")
xmlRoot.append(child)
tree.write("/tmp/" + executionID + ".xml")
If you must use an open file to prettify the XML, use the 'w'
mode, 'a'
opens a file for appending, leading to the behaviour you observed:
with open("/tmp/" + executionID + ".xml", 'w') as output:
output.write(prettify(tree))
where prettify
is something along the lines of:
from xml.etree import ElementTree from xml.dom import minidom def prettify(elem): "" "Return a pretty-printed XML string for the Element. "" " rough_string = ElementTree.tostring(elem, 'utf-8') reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent = " ")
25/05/2022
import xml.etree.ElementTree as ET
root = ET.Element("Root")
root = ET.Element("Root") m1 = ET.Element("child1") root.append(m1) b1 = ET.SubElement(m1, "subelement1") b1.text = "value1"
b2 = ET.SubElement(m1, "subelement2") b2.text = "value2"
m2 = ET.Element("child2") root.append(m2) c1 = ET.SubElement(m2, "subelement3") c1.text = "value3"
c2 = ET.SubElement(m2, "subelement3") c2.text = "value4"
tree = ET.ElementTree(root)
with open(your_file_name.xml ', "w") as files: tree.write(files)
f = open(your_file_name.xml ', "wb")tree.write(f)
ElementTree provides a simple way to build XML documents and write them to files. The ElementTree.write() method serves this purpose.,Replaces the root element for this tree. This discards the current contents of the tree, and replaces it with the given element. Use with care. element is an element instance.,XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. ET has two classes for this purpose - ElementTree represents the whole XML document as a tree, and Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level.,Element class. This class defines the Element interface, and provides a reference implementation of this interface.
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N" />
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W" />
<neighbor name="Colombia" direction="E" />
</country>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
root = ET.fromstring(country_data_as_string)
>>> root.tag 'data' >>>
root.attrib {}
>>>
for child in root:
...print(child.tag, child.attrib)
...
country {
'name': 'Liechtenstein'
}
country {
'name': 'Singapore'
}
country {
'name': 'Panama'
}
>>> root[0][1].text '2008'
ElementTree is a class that wraps the element structure and allows conversion to and from XML. Let us now try to parse the above XML file using python module.,The elements present your XML file can be manipulated. To do this, you can use the set() function. Let us first take a look at how to add something to XML.,The above code will return the same output as the previous one. Please note that the XML document used as a string is just one part of ‘Sample.xml’ which I have used for better visibility. You can use the complete XML document as well.,The above example shows the contents of a file which I have named as ‘Sample.xml’ and I will be using the same in this Python XML parser tutorial for all the upcoming examples.
EXAMPLE:
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<food>
<item name="breakfast">Idly</item>
<price>$2.5</price>
<description>
Two idly's with chutney
</description>
<calories>553</calories>
</food>
<food>
<item name="breakfast">Paper Dosa</item>
<price>$2.7</price>
<description>
Plain paper dosa with chutney
</description>
<calories>700</calories>
</food>
<food>
<item name="breakfast">Upma</item>
<price>$3.65</price>
<description>
Rava upma with bajji
</description>
<calories>600</calories>
</food>
<food>
<item name="breakfast">Bisi Bele Bath</item>
<price>$4.50</price>
<description>
Bisi Bele Bath with sev
</description>
<calories>400</calories>
</food>
<food>
<item name="breakfast">Kesari Bath</item>
<price>$1.95</price>
<description>
Sweet rava with saffron
</description>
<calories>950</calories>
</food>
</metadata>
import xml.etree.ElementTree as ET
mytree = ET.parse('sample.xml')
myroot = mytree.getroot()
print(myroot)
You can also use fromstring() function to parse your string data. In case you want to do this, pass your XML as a string within triple quotes as follows:
import xml.etree.ElementTree as ET
data='''
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<food>
<item name="breakfast">Idly</item>
<price>$2.5</price>
<description>
Two idly's with chutney
</description>
<calories>553</calories>
</food>
</metadata>
'''
myroot = ET.fromstring(data)
#print(myroot)
print(myroot.tag)
for x in myroot.findall('food'):
item = x.find('item').text
price = x.find('price').text
print(item, price)