Python XML Processing
What is XML ?XML stands for eXtensible Markup Language . The XML standard is a flexible way to create information formats and electronically share structured data via the public Internet, as well as via corporate networks.
XML ParserThe Document Object Model (DOM) defines a standard for accessing and manipulating documents. The XML DOM defines a standard way for accessing and manipulating XML documents. It presents an XML document as a tree-structure.
XML parsing in PythonPython can parse xml documents in several ways. It has traditional dom and sax parsers. This chapter will focus on using inbuilt xml module in python for parsing XML.
Sample XML document
<data> <items> <item name="product1"></item> <item name="product2"></item> <item name="product3"></item> <item name="product4"></item> <item name="product5"></item> </items> </data>Copy and paste the above xml code in a text file and save it as "data.xml" in working directory.
ElementTree XML APIThe xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. The Element type is a flexible container object, designed to store hierarchical data structures in memory. example
import xml.etree.ElementTree doc = xml.etree.ElementTree.parse('data.xml').getroot() for elem in doc.findall('items/item'): print (elem.get('name'))output
product1 product2 product3 product4 product5
Minimal DOM implementation(xml.dom.minidom)DOM Example The xml.dom.minidom is a minimal implementation of the Document Object Model interface, with an API similar to that in other languages. It is intended to be simpler than the full DOM and also significantly smaller. Programmers who are not already proficient with the DOM should consider using the xml.etree.ElementTree module for their XML processing instead. example
from xml.dom import minidom xmldoc = minidom.parse('data.xml') product_list = xmldoc.getElementsByTagName('item') print("No of Items : ", len(product_list)) for product in product_list: print(product.attributes['name'].value)output
No of Items : 5 product1 product2 product3 product4 product5
- How to use Date and Time in Python
- Python Exception Handling
- How to Generate a Random Number in Python
- How to pause execution of program in Python
- How to read and write a CSV files with Python
- Threads and Threading in Python
- Python Multithreaded Programming
- Python range() function
- How to Convert a Python String to int
- Python filter() Function
- Difference between range() and xrange() in Python
- How to print without newline in Python?
- How to remove spaces from a string in Python
- How to get the current time in Python
- Slicing in Python
- Create a nested directory without exception | Python
- How to measure time taken between lines of code in python?
- How to concatenate two lists in Python
- How to Find the Mode in a List Using Python
- Difference Between Static and Class Methods in Python?