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:
|Tag||String identifying what kind of data the element represents.
Can be accessed using elementname.tag.
|Number of Atrributes||Strored 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.|
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.
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.
Modifying the XML document can also be done through Element 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.
- Working with XML Files in R Programming
- Parsing XML with DOM APIs in Python
- Parse XML using Minidom in Python
- Turning a Dictionary into XML in Python
- How to convert XML file into array in PHP?
- XML Full Form
- How to generate an XML file dynamically using PHP?
- Writing CSV files in Python
- How to open a PDF files in web browser using PHP?
- Creating a dataframe using Excel files
- Deleting all files from a folder using PHP
- How to include() all PHP files from a directory ?
- Perl | Creating Excel Files
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
- How to upload files asynchronously using jQuery?
- How to select and upload multiple files with HTML and PHP, using HTTP POST?
- Perl | Reading Excel Files
- How to count files in a directory using PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.