In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.