ডকুমেন্ট অবজেক্ট মডেল ("DOM") হল ওয়ার্ল্ড ওয়াইড ওয়েব কনসোর্টিয়াম (W3C) থেকে XML নথিগুলি অ্যাক্সেস এবং পরিবর্তন করার জন্য একটি ক্রস-ভাষা API৷
এলোমেলো-অ্যাক্সেস অ্যাপ্লিকেশনের জন্য DOM অত্যন্ত উপযোগী। SAX আপনাকে একবারে নথির একটি বিট দেখার অনুমতি দেয়। আপনি যদি একটি SAX উপাদানের দিকে তাকান তবে অন্যটিতে আপনার কোনো অ্যাক্সেস নেই৷
৷একটি XML নথি দ্রুত লোড করার এবং xml.dom মডিউল ব্যবহার করে একটি মিনিডম অবজেক্ট তৈরি করার সবচেয়ে সহজ উপায় এখানে। মিনিডম অবজেক্ট একটি সহজ পার্সার পদ্ধতি প্রদান করে যা দ্রুত XML ফাইল থেকে একটি DOM ট্রি তৈরি করে।
নমুনা বাক্যাংশটি মিনিডম অবজেক্টের পার্স (ফাইল [,পার্সার]) ফাংশনকে একটি DOM ট্রি অবজেক্টে ফাইল দ্বারা মনোনীত XML ফাইলকে পার্স করার জন্য কল করে।
#!/usr/bin/python
from xml.dom.minidom import parse
import xml.dom.minidom
# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("movies.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("shelf"):
print "Root element : %s" % collection.getAttribute("shelf")
# Get all the movies in the collection
movies = collection.getElementsByTagName("movie")
# Print detail of each movie.
for movie in movies:
print "*****Movie*****"
if movie.hasAttribute("title"):
print "Title: %s" % movie.getAttribute("title")
type = movie.getElementsByTagName('type')[0]
print "Type: %s" % type.childNodes[0].data
format = movie.getElementsByTagName('format')[0]
print "Format: %s" % format.childNodes[0].data
rating = movie.getElementsByTagName('rating')[0]
print "Rating: %s" % rating.childNodes[0].data
description = movie.getElementsByTagName('description')[0]
print "Description: %s" % description.childNodes[0].data এটি নিম্নলিখিত ফলাফল তৈরি করবে -
Root element : New Arrivals *****Movie***** Title: Enemy Behind Type: War, Thriller Format: DVD Rating: PG Description: Talk about a US-Japan war *****Movie***** Title: Transformers Type: Anime, Science Fiction Format: DVD Rating: R Description: A schientific fiction *****Movie***** Title: Trigun Type: Anime, Action Format: DVD Rating: PG Description: Vash the Stampede! *****Movie***** Title: Ishtar Type: Comedy Format: VHS Rating: PG Description: Viewable boredom
DOM API ডকুমেন্টেশনের সম্পূর্ণ বিশদ বিবরণের জন্য, অনুগ্রহ করে স্ট্যান্ডার্ড Python APIs দেখুন।