One
Python has three ways to parse Xml,sax,dom, and ElementTree
1.SAX (simple API for XML): event-based
The Pyhton standard library contains sax parsers, which are a typical, extremely fast tool that does not consume a lot of memory when parsing XML.
2.DOM(Document object Model): Object-based
Compared to sax, the typical disadvantage of DOM is that it is slower and consumes more memory because the DOM reads the entire XML number into memory and establishes an object for the first node in the tree. The advantage of using the DOM is that you don't need to track the status because each node knows who it is and who is the child node. But Dom is a little tricky to use.
3.elementtreE (element tree)
ElementTree is like a lightweight dom, with a convenient and friendly API. Good code availability, fast speed, low memory consumption.
XML instance file Movies.xml:
<CollectionShelf= "New Arrivals"><movietitle= "Enemy Behind"> <type>War, Thriller</type> <format>Dvd</format> < Year>2003</ Year> <rating>Pg</rating> <stars>10</stars> <Description>Talk about a Us-japan war</Description></movie><movietitle= "Transformers"> <type>Anime, science fiction</type> <format>Dvd</format> < Year>1989</ Year> <rating>R</rating> <stars>8</stars> <Description>A schientific Fiction</Description></movie></Collection>
1. Sax parsing
(1) ContentHandler class method:
The startdocument () method is called when the document is started.
Called when the Enddocument () method parser reaches the end of the document.
The startelement (name, Attrs) method is called when it encounters an XML start tag.
Name is the name of the tag, and Attrs is the dictionary of the property value of the tag.
Called when the endElement (name) method encounters an XML end tag.
characters (content) method Call Timing:
Starting with a row, there are characters before the label is encountered, and the content value is these strings.
From a label, before encountering the next label, 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 label can be either the start tag or the end tag.
(2)Make_parser method
Xml.sax.make_parser ([parser_list]) #以下方法创建一个新的解析器对象并返回.
- Parser_list-Optional parameters, parser list
(3)ParseString method
Xml.sax.parseString (xmlstring, contenthandler[, ErrorHandler]) #创建一个XML解析器并解析xml字符串
- Xmlstring-xml string
- ContentHandler-Must be an object of ContentHandler
- ErrorHandler-If this argument is specified, ErrorHandler must be a sax ErrorHandler object
Instance:
ImportXml.saxclassMoviehandler (xml.sax.ContentHandler):def __init__(self): self. CurrentData=""Self.type=""Self.format=""Self.year=""self.rating=""Self.stars=""self.description="" #element starts calling defstartelement (self, Tag, attributes): self. CurrentData=TagifTag = ="movie": Print("*****movie*****") Title= attributes["title"] Print("Title:", title)#element End Call defendElement (self, tag):ifSelf. CurrentData = ="type": Print("Type:", Self.type)elifSelf. CurrentData = ="format": Print("Format:", Self.format)elifSelf. CurrentData = =" Year": Print("Year :", Self.year)elifSelf. CurrentData = ="rating": Print("Rating:", self.rating)elifSelf. CurrentData = ="stars": Print("Stars:", Self.stars)elifSelf. CurrentData = ="Description": Print("Description:", self.description) self. CurrentData="" #called when a character is read defcharacters (self, content):ifSelf. CurrentData = ="type": Self.type=contentelifSelf. CurrentData = ="format": Self.format=contentelifSelf. CurrentData = =" Year": Self.year=contentelifSelf. CurrentData = ="rating": self.rating=contentelifSelf. CurrentData = ="stars": Self.stars=contentelifSelf. CurrentData = ="Description": Self.description=contentif(__name__=="__main__"): #Create a XMLReaderParser =Xml.sax.make_parser ()#Turn off Namepsacesparser.setfeature (xml.sax.handler.feature_namespaces, 0)#rewrite ContexthandlerHandler =Moviehandler () Parser.setcontenthandler (Handler) parser.parse ("E:/movies.xml")
2. Dom parsing
3, ElementTree analysis
For details, please see:
Http://www.cnblogs.com/hongfei/p/python-xml-sax.html#pricelist
XML parsing (python3.4)