Python Tutorial ( PDFDrive )
The parse Method: Following method creates a SAX parser and uses it to parse a document.xml.sax.parse( xmlfile, contenthandler[, errorhandler])Here is the detail of the parameters:• xmlfile: This is the name of the XML file to read from.• contenthandler: This must be a ContentHandler object.• errorhandler: If specified, errorhandler must be a SAX ErrorHandler object.The parseString Method: There is one more method to create a SAX parser and to parse the specified XML string.xml.sax.parseString(xmlstring, contenthandler[, errorhandler])Here is the detail of the parameters:• xmlstring: This is the name of the XML string to read from.• contenthandler: This must be a ContentHandler object.• errorhandler: If specified, errorhandler must be a SAX ErrorHandler object.EXAMPLE: #!/usr/bin/pythonimport xml.saxclass MovieHandler( xml.sax.ContentHandler ):def __init__(self):self.CurrentData = ""self.type = ""self.format = ""self.year = ""self.rating = ""self.stars = ""self.description = ""# Call when an element startsdef startElement(self, tag, attributes):self.CurrentData = tagif tag == "movie":print "*****Movie*****"title = attributes["title"]print "Title:", title# Call when an elements endsdef endElement(self, tag):if self.CurrentData == "type":print "Type:", self.typeelif self.CurrentData == "format":print "Format:", self.formatelif self.CurrentData == "year":print "Year:", self.yearelif self.CurrentData == "rating":TUTORIALS POINT Simply Easy Learning
print "Rating:", self.ratingelif self.CurrentData == "stars":print "Stars:", self.starselif self.CurrentData == "description":print "Description:", self.descriptionself.CurrentData = ""# Call when a character is readdef characters(self, content):if self.CurrentData == "type":self.type = contentelif self.CurrentData == "format":self.format = contentelif self.CurrentData == "year":self.year = contentelif self.CurrentData == "rating":self.rating = contentelif self.CurrentData == "stars":self.stars = contentelif self.CurrentData == "description":self.description = contentif ( __name__ == "__main__"):# create an XMLReaderparser = xml.sax.make_parser()# turn off namepsacesparser.setFeature(xml.sax.handler.feature_namespaces, 0)# override the default ContextHandlerHandler = MovieHandler()parser.setContentHandler( Handler )parser.parse("movies.xml")This would produce the following result:*****Movie*****Title: Enemy BehindType: War, ThrillerFormat: DVDYear: 2003Rating: PGStars: 10Description: Talk about a US-Japan war*****Movie*****Title: TransformersType: Anime, Science FictionFormat: DVDYear: 1989Rating: RStars: 8Description: A schientific fiction*****Movie*****Title: TrigunType: Anime, ActionFormat: DVDRating: PGStars: 10Description: Vash the Stampede!*****Movie*****TUTORIALS POINT Simply Easy Learning
- Page 313 and 314: For the above class let's try to ac
- Page 315 and 316: print 'Calling child method'c = Chi
- Page 317 and 318: Data Hiding: An object's attribute
- Page 319 and 320: groups()This method returns all mat
- Page 321 and 322: SYNTAX: re.sub(pattern, repl, strin
- Page 323 and 324: \s Matches whitespace. Equivalent t
- Page 325 and 326: Backreferences: This matches a prev
- Page 327 and 328: CHAPTER20Python CGI ProgrammingWhat
- Page 329 and 330: If you click hello.py, then this pr
- Page 331 and 332: Simple URL Example : Get Metho
- Page 333 and 334: </form>The result of this code is t
- Page 335 and 336: text_content = form.getvalue('textc
- Page 337 and 338: Here is an example of how to retrie
- Page 339 and 340: CHAPTER21Python Database AccessThe
- Page 341 and 342: EXAMPLE: Following is the example o
- Page 343 and 344: ('Mac', 'Mohan', 20, 'M', 2000)try:
- Page 345 and 346: DELETE Operation: DELETE operation
- Page 347 and 348: ProgrammingErrorSubclass of Databas
- Page 349 and 350: The socket Module: To create a so
- Page 351 and 352: Protocol Common function Port No Py
- Page 353 and 354: Subject: SMTP e-mail testThis is a
- Page 355 and 356: try:smtpObj = smtplib.SMTP('localho
- Page 357 and 358: count = 0while count < 5:time.sleep
- Page 359 and 360: Synchronizing Threads: The threadi
- Page 361 and 362: thread = myThread(threadID, tName,
- Page 363: <stars>10</stars><description>Talk
- Page 367 and 368: Format: DVDRating: PGDescription: T
- Page 369 and 370: Tkinter Widgets Tkinter provides v
- Page 371 and 372: fgfontheighthighlightcolorimagejust
- Page 373 and 374: WidthXscrollincrementXscrollcommand
- Page 375 and 376: pattern when it is over the checkbu
- Page 377 and 378: The Entry widget is used to accept
- Page 379 and 380: E1 = Entry(top, bd =5)E1.pack(side
- Page 381 and 382: Optionanchorbgbitmapbdcursorfontfgh
- Page 383 and 384: selectmodeDetermines how many items
- Page 385 and 386: Syntax: Here is the simple syntax t
- Page 387 and 388: Menu The goal of this widget is to
- Page 389 and 390: filemenu.add_command(label="Exit",
- Page 391 and 392: Example: Try the following example
- Page 393 and 394: WidthWraplengthWidth of the label i
- Page 395 and 396: label appears in the top left corne
- Page 397 and 398: Parameters: • master: This repres
- Page 399 and 400: Parameters: • master: This repres
- Page 401 and 402: mark_gravity(mark [,gravity])Return
- Page 403 and 404: Methods & Descriptiondeiconify()Dis
- Page 405 and 406: disabledbackgrounddisabledforegroun
- Page 407 and 408: bgbdThe color of the slider and arr
- Page 409 and 410: fontheightlabelAnchorhighlightbackg
- Page 411 and 412: Standard attributes:Let's take a l
- Page 413 and 414: • family: The font family name as
print "Rating:", self.rating
elif self.CurrentData == "stars":
print "Stars:", self.stars
elif self.CurrentData == "description":
print "Description:", self.description
self.CurrentData = ""
# Call when a character is read
def characters(self, content):
if self.CurrentData == "type":
self.type = content
elif self.CurrentData == "format":
self.format = content
elif self.CurrentData == "year":
self.year = content
elif self.CurrentData == "rating":
self.rating = content
elif self.CurrentData == "stars":
self.stars = content
elif self.CurrentData == "description":
self.description = content
if ( __name__ == "__main__"):
# create an XMLReader
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
# override the default ContextHandler
Handler = MovieHandler()
parser.setContentHandler( Handler )
parser.parse("movies.xml")
This would produce the following result:
*****Movie*****
Title: Enemy Behind
Type: War, Thriller
Format: DVD
Year: 2003
Rating: PG
Stars: 10
Description: Talk about a US-Japan war
*****Movie*****
Title: Transformers
Type: Anime, Science Fiction
Format: DVD
Year: 1989
Rating: R
Stars: 8
Description: A schientific fiction
*****Movie*****
Title: Trigun
Type: Anime, Action
Format: DVD
Rating: PG
Stars: 10
Description: Vash the Stampede!
*****Movie*****
TUTORIALS POINT
Simply Easy Learning