![]() |
VOOZH | about |
Python|Modifying/Parsing XML
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.The design goals of XML focus on simplicity, generality, and usability across the Internet.It is a textual data format with strong support via Unicode for different human languages. Although the design of XML focuses on documents, the language is widely used for the representation of arbitrary data structures such as those used in web services.
XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree.To perform any operations like parsing, searching, modifying an XML file we use a module xml.etree.ElementTree .It has two classes.ElementTree represents the whole XML document as a tree which helps while performing the operations. Element represents a single node in this tree.Reading and writing from the whole document are done on the ElementTree level.Interactions with a single XML element and its sub-elements are done on the Element level.
Properties of Element:
| Properties | Description |
|---|---|
| Tag | String identifying what kind of data the element represents. Can be accessed using elementname.tag. |
| Number of Attributes | Stored as a python dictionary. Can be accesses by elementname.attrib. |
| Text string | String information regarding the element. |
| Child string | Optional child elements string information. |
| Child Elements | Number of child elements to a particular root. |
PARSING:
We can parse XML data from a string or an XML document.Considering xml.etree.ElementTree as ET.
1. ET.parse('Filename').getroot() -ET.parse('fname')-creates a tree and then we extract the root by .getroot().
2. ET.fromstring(stringname) -To create a root from an XML data string.
Example 1:
XML document:
Python Code:
Output:
Element methods:
1)Element.iter('tag') -Iterates over all the child elements(Sub-tree elements)
2)Element.findall('tag') -Finds only elements with a tag which are direct children of the current element.
3)Element.find('tag') -Finds the first Child with the particular tag.
4)Element.get('tag') -Accesses the elements attributes.
5)Element.text -Gives the text of the element.
6)Element.attrib-returns all the attributes present.
7)Element.tag-returns the element name.
Example 2:
Output:
MODIFYING:
Modifying the XML document can also be done through Element methods.
Methods:
1)Element.set('attrname', 'value') - Modifying element attributes.
2)Element.SubElement(parent, new_childtag) -creates a new child tag under the parent.
3)Element.write('filename.xml')-creates the tree of xml into another file.
4)Element.pop() -delete a particular attribute.
5)Element.remove() -to delete a complete tag.
Example 3:
XML Document:
Python Code: