The Python standard library contains sax parsers, and Sax uses event-driven models to process XML files by triggering events and invoking user-defined callback functions during parsing of XML
Using sax in Python to process XML first introduces the parse function in Xml.sax and the ContentHandler in Xml.sax.handler
Introduction to ContentHandler class methods
# characters (content) method
# Call Time:
# Starting with the line, before encountering the label, there are characters, and the content value is these strings.
# from a label, before encountering the next tag, there are characters, content values for these strings.
# from a label that encounters a line terminator, there are characters, content values for these strings.
The # tag can be the start tag or the end tag.
# startdocument () method
# Called when the document is started.
# enddocument () method
# Called when the parser reaches the end of the document.
# startelement (name, Attrs) method
# called when an XML start tag is encountered, name is the name of the tag, and Attrs is a dictionary of the property values of the tag.
# endElement (name) method
# called when an XML end tag is encountered.
# Make_parser Method
# Create a new parser object and return.
# Xml.sax.make_parser ([parser_list]) # (parser_list-optional argument, parser list)
# parser method
# Create a SAX parser and parse the XML document:
# Xml.sax.parse (xmlfile, contenthandler[, ErrorHandler])
# parameter Description:
# xmlfile-xml file name
# ContentHandler-Must be an object of ContentHandler
# ErrorHandler-If this argument is specified, ErrorHandler must be a sax ErrorHandler object
# ParseString Method
# ParseString method creates an XML parser and parses an XML string
# xml.sax.parseString (xmlstring, contenthandler[, ErrorHandler])
# parameter Description:
# Xmlstring-xml String
# ContentHandler-Must be an object of ContentHandler
# ErrorHandler-If this argument is specified, ErrorHandler must be a sax ErrorHandler object
# Eg_v1import Xml.saxclass Moviehandler (xml.sax.ContentHandler): def __init__ (self): self. CurrentData = "Self.type =" "Self.format =" "Self.year =" "self.rating =" "Self.s Tars = "Self.description =" "# Start element processing def startelement (self, Tag, attributes): self. CurrentData = Tag if tag = = "Movie": Print ("*****movie*****") title = attributes["title"] Print ("title:", title) # element finishes processing Def endElement (self, tag): if self. CurrentData = = "Type": Print ("type:", Self.type) elif self. CurrentData = = "format": Print ("Format:", Self.format) elif self. CurrentData = = "Year": Print (' year: ', self.year) elif self. CurrentData = = "rating": Print ("rating:", self.rating) elif self. CurrentData = = "Stars": Print ("stars:", Self.stars) elif self. CurrentData = = "description": Print ("description:", self.)Description) # content event handling 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 =contentif (__name__ = = "__main__"): Parser = Xml.sax.make_pa Rser () parser.setfeature (xml.sax.handler.feature_namespaces,0) Headler = Moviehandler () Parser.setContentHandler ( Headler) parser.parse ("Movies.xml") # *****movie*****# Title:enemy behind# Type:war, thriller# format:dvd# year:20 03# rating:pg# stars:10# Description:talk about a us-japan war# description:## *****movie*****# title:transformers# ty Pe:anime, Science fiction# format:dvd# year:1989# rating:r# stars:8# desCription:a schientific fiction# description:## *****movie*****# title:trigun# type:anime, Action# format:dvd# Rating: pg# stars:10# Description:vash The stampede!# description:## *****movie*****# title:ishtar# type:comedy# format:vhs# rating:pg# stars:2# description:viewable boredom# Description:
Python parsing xml using sax