दस्तावेज़ ऑब्जेक्ट मॉडल ("DOM") XML दस्तावेज़ों तक पहुँचने और संशोधित करने के लिए वर्ल्ड वाइड वेब कंसोर्टियम (W3C) का एक क्रॉस-लैंग्वेज 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
डीओएम एपीआई दस्तावेज पर पूरी जानकारी के लिए, कृपया मानक पायथन एपीआई देखें।