XML কি?
XML (Extensible Markup Language) হলো ডেটা স্টোর ও ট্রান্সপোর্টের জন্য একটি মার্কআপ ল্যাঙ্গুয়েজ। এতে এলিমেন্ট থাকে শুরু ট্যাগ (<tag>) এবং শেষ ট্যাগ (</tag>) দিয়ে। এলিমেন্টের ভেতর চাইল্ড এলিমেন্ট বা টেক্সট কন্টেন্ট থাকতে পারে।
নমুনা XML ফাইল (TutorialsList.xml)
<?xml version="1.0"?>
<Tutorials>
<Tutorial id="Tu101">
<author>Vicky, Matthew</author>
<title>Geo-Spatial Data Analysis</title>
<stream>Python</stream>
<price>4.95</price>
<publish_date>2020-07-01</publish_date>
<description>Learn geo Spatial data Analysis using Python.</description>
</Tutorial>
<Tutorial id="Tu102">
<author>Bolan, Kim</author>
<title>Data Structures</title>
<stream>Computer Science</stream>
<price>12.03</price>
<publish_date>2020-1-19</publish_date>
<description>Learn Data structures using different programming lanuages.</description>
</Tutorial>
<Tutorial id="Tu103">
<author>Sora, Everest</author>
<title>Analytics using Tensorflow</title>
<stream>Data Science</stream>
<price>7.11</price>
<publish_date>2020-1-19</publish_date>
<description>Learn Data analytics using Tensorflow.</description>
</Tutorial>
</Tutorials>
১. xml.etree.ElementTree দিয়ে পড়া (সহজ ও দ্রুত)
import xml.etree.ElementTree as ET
xml_tree = ET.parse('TutorialsList.xml')
xml_root = xml_tree.getroot()
print("Tutorial List:")
for tutorial in xml_root:
for child in tutorial:
print(child.text) # টেক্সট কন্টেন্ট
আউটপুট
Vicky, Matthew
Geo-Spatial Data Analysis
Python
4.95
2020-07-01
Learn geo Spatial data Analysis using Python.
Bolan, Kim
Data Structures
Computer Science
12.03
2020-1-19
Learn Data structures using different programming lanuages.
Sora, Everest
Analytics using Tensorflow
Data Science
7.11
2020-1-19
Learn Data analytics using Tensorflow.
২. XML অ্যাট্রিবিউট পাওয়া
import xml.etree.ElementTree as ET
xml_tree = ET.parse('TutorialsList.xml')
xml_root = xml_tree.getroot()
for tutorial in xml_root.iter('Tutorial'):
print(tutorial.attrib) # {'id': 'Tu101'} ইত্যাদি
আউটপুট
{'id': 'Tu101'}
{'id': 'Tu102'}
{'id': 'Tu103'}
৩. ফিল্টারিং (XPath-লাইক সিনট্যাক্স)
# Price = 12.03 वाले Tutorial খুঁজুন
for tutorial in xml_root.findall("./Tutorial/[price='12.03']"):
print(tutorial.attrib) # {'id': 'Tu102'}
৪. xml.dom.minidom দিয়ে DOM পার্সিং (বড় ফাইলের জন্য কম সাবধান)
from xml.dom.minidom import parse
import xml.dom.minidom
DOMTree = xml.dom.minidom.parse('TutorialsList.xml')
collection = DOMTree.documentElement
tut_list = collection.getElementsByTagName("Tutorial")
print("***** Tutorials *****")
for tut in tut_list:
stream = tut.getElementsByTagName('stream')[0]
print("Stream:", stream.childNodes[0].data)
price = tut.getElementsByTagName('price')[0]
print("Price:", price.childNodes[0].data)
আউটপুট
***** Tutorials *****
Stream: Python
Price: 4.95
Stream: Computer Science
Price: 12.03
Stream: Data Science
Price: 7.11
কোনটি ব্যবহার করবেন?
- ElementTree: হালকা, দ্রুত, পাইথনিক API, অধিকাংশ কাজের জন্য আদর্শ।
- minidom: DOM API পছন্দ করলে, কিন্তু মেমরি বেশি খায় এবং স্লো।
- lxml (থার্ড পার্টি): খুব দ্রুত, XPath ১.০/২.০ সাপোর্ট, বড় XML এর জন্য সেরা।