How python operates xml
The editor will share with you how python operates xml. I hope you will get something after reading this article. Let's discuss it together.
Description
1. DOM will read the whole XML into memory and parse it into a tree, so it takes up a lot of memory and parses slowly.
Its advantage is that it can traverse the nodes of the tree at will.
2. SAX is a stream mode, which takes up small memory and fast analysis while reading and analyzing. The disadvantage is that you need to deal with events on your own.
In general, SAX is preferred because DOM takes up too much memory.
Example
From xml.parsers.expat import ParserCreate class DefaultSaxHandler (object): def start_element (self, name, attrs): print ('sax:start_element:% s, attrs:% s'% (name, str (attrs)) def end_element (self, name): print ('sax:end_element:% s'% name) def char_data (self Text): print ('sax:char_data:% s'% text) xml = ritual 'Python Ruby''' handler = DefaultSaxHandler () parser = ParserCreate () parser.StartElementHandler = handler.start_elementparser.EndElementHandler = handler.end_elementparser.CharacterDataHandler = handler.char_dataparser.Parse (xml) / / Test result sax:start_element: ol, attrs: {} sax:char_data: sax:char_data: sax:start_element: li Attrs: {} sax:start_element: a, attrs: {'href':' / python'} sax:char_data: Pythonsax:end_element: asax:end_element: lisax:char_data: sax:start_element: li, attrs: {} sax:start_element: a Attrs: {'href':' / ruby'} sax:char_data: Rubysax:end_element: asax:end_element: lisax:char_data: sax:end_element: ol has finished reading this article I believe you have a certain understanding of "how python operates xml". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!