{"id":28545,"date":"2024-11-25T17:46:10","date_gmt":"2024-11-25T09:46:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28545\/"},"modified":"2024-11-25T17:46:10","modified_gmt":"2024-11-25T09:46:10","slug":"python%e4%b9%8bxml%e8%a7%a3%e6%9e%90%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28545\/","title":{"rendered":"Python\u4e4bXML\u89e3\u6790\u8be6\u89e3"},"content":{"rendered":"<p><\/p>\n<p><\/p>\n<h2>\u4ec0\u4e48\u662f\uff1f<\/h2>\n<p>xml \u6307\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\uff08e<strong>x<\/strong>tensible <strong>m<\/strong>arkup <strong>l<\/strong>anguage\uff09\u3002<\/p>\n<p>XML \u88ab\u8bbe\u8ba1\u7528\u6765\u4f20\u8f93\u548c\u5b58\u50a8\u6570\u636e\u3002<\/p>\n<p>XML\u662f\u4e00\u5957\u5b9a\u4e49\u8bed\u4e49\u6807\u8bb0\u7684\u89c4\u5219\uff0c\u8fd9\u4e9b\u6807\u8bb0\u5c06\u6587\u6863\u5206\u6210\u8bb8\u591a\u90e8\u4ef6\u5e76\u5bf9\u8fd9\u4e9b\u90e8\u4ef6\u52a0\u4ee5\u6807\u8bc6\u3002<\/p>\n<p>\u5b83\u4e5f\u662f\u5143\u6807\u8bb0\u8bed\u8a00\uff0c\u5373\u5b9a\u4e49\u4e86\u7528\u4e8e\u5b9a\u4e49\u5176\u4ed6\u4e0e\u7279\u5b9a\u9886\u57df\u6709\u5173\u7684\u3001\u8bed\u4e49\u7684\u3001\u7ed3\u6784\u5316\u7684\u6807\u8bb0\u8bed\u8a00\u7684\u53e5\u6cd5\u8bed\u8a00\u3002<\/p>\n<hr>\n<h2> \u5bf9XML\u7684\u89e3\u6790<\/h2>\n<p>\u5e38\u89c1\u7684XML\u6709DOM\u548cSAX\uff0c\u8fd9\u4e24\u79cd\u63a5\u53e3\u5904\u7406XML\u6587\u4ef6\u7684\u65b9\u5f0f\u4e0d\u540c\uff0c\u5f53\u7136\u4f7f\u7528\u573a\u5408\u4e5f\u4e0d\u540c\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>python\u6709\u4e09\u79cd\u65b9\u6cd5\u89e3\u6790XML\uff0cSAX\uff0cDOM\uff0c\u4ee5\u53caElementTree:<\/p>\n<h3>1.SAX (simple   XML )<\/h3>\n<p>python \u6807\u51c6\u5e93\u5305\u542bSAX\u89e3\u6790\u5668\uff0cSAX\u7528\uff0c\u901a\u8fc7\u5728\u89e3\u6790XML\u7684\u8fc7\u7a0b\u4e2d\u89e6\u53d1\u4e00\u4e2a\u4e2a\u7684\u4e8b\u4ef6\u5e76\u8c03\u7528\u7528\u6237\u5b9a\u4e49\u7684\u6765\u5904\u7406XML\u6587\u4ef6\u3002<\/p>\n<h3>2.DOM(  )<\/h3>\n<p>\u5c06XML\u6570\u636e\u5728\u5185\u5b58\u4e2d\u89e3\u6790\u6210\u4e00\u4e2a\u6811\uff0c\u901a\u8fc7\u5bf9\u6811\u7684\u64cd\u4f5c\u6765\u64cd\u4f5cXML\u3002<\/p>\n<h3>3.ElementTree(\u5143\u7d20\u6811)<\/h3>\n<p>ElementTree\u5c31\u50cf\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684DOM\uff0c\u5177\u6709\u65b9\u4fbf\u53cb\u597d\u7684API\u3002\u4ee3\u7801\u53ef\u7528\u6027\u597d\uff0c\u901f\u5ea6\u5feb\uff0c\u6d88\u8017\u5185\u5b58\u5c11\u3002<\/p>\n<p><strong>\u6ce8\uff1a<\/strong>\u56e0DOM\u9700\u8981\u5c06XML\u6570\u636e\u6620\u5c04\u5230\u5185\u5b58\u4e2d\u7684\u6811\uff0c\u4e00\u662f\u6bd4\u8f83\u6162\uff0c\u4e8c\u662f\u6bd4\u8f83\u8017\u5185\u5b58\uff0c\u800cSAX\u6d41\u5f0f\u8bfb\u53d6XML\u6587\u4ef6\uff0c\u6bd4\u8f83\u5feb\uff0c\u5360\u7528\u5185\u5b58\u5c11\uff0c\u4f46\u9700\u8981\u7528\u6237\u5b9e\u73b0\u56de\u8c03\u51fd\u6570\uff08haner\uff09\u3002<\/p>\n<p>\u672c\u7ae0\u8282\u4f7f\u7528\u5230\u7684XML\u5b9e\u4f8b\u6587\u4ef6movies.xml\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;collection&gt;&lt;movie&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;type&gt;War,&nbsp;Thriller&lt;\/type&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;format&gt;DVD&lt;\/format&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;year&gt;2003&lt;\/year&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;rating&gt;PG&lt;\/rating&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;stars&gt;10&lt;\/stars&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;description&gt;Talk&nbsp;about&nbsp;a&nbsp;US-Japan&nbsp;war&lt;\/description&gt;&lt;\/movie&gt;&lt;movie&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;type&gt;Anime,&nbsp;Science&nbsp;Fiction&lt;\/type&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;format&gt;DVD&lt;\/format&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;year&gt;1989&lt;\/year&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;rating&gt;R&lt;\/rating&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;stars&gt;8&lt;\/stars&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;description&gt;A&nbsp;schientific&nbsp;fiction&lt;\/description&gt;&lt;\/movie&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;movie&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;type&gt;Anime,&nbsp;Action&lt;\/type&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;format&gt;DVD&lt;\/format&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;episodes&gt;4&lt;\/episodes&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;rating&gt;PG&lt;\/rating&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;stars&gt;10&lt;\/stars&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;description&gt;Vash&nbsp;the&nbsp;Stampede!&lt;\/description&gt;&lt;\/movie&gt;&lt;movie&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;type&gt;Comedy&lt;\/type&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;format&gt;VHS&lt;\/format&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;rating&gt;PG&lt;\/rating&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;stars&gt;2&lt;\/stars&gt;\r\n&nbsp;&nbsp;&nbsp;&lt;description&gt;Viewable&nbsp;boredom&lt;\/description&gt;&lt;\/movie&gt;&lt;\/collection&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h2>python\u4f7f\u7528SAX\u89e3\u6790xml<\/h2>\n<p>SAX\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684API\u3002<\/p>\n<p>\u5229\u7528SAX\u89e3\u6790XML\u6587\u6863\u7275\u6d89\u5230\u4e24\u4e2a\u90e8\u5206:\u89e3\u6790\u5668\u548c\u5668\u3002<\/p>\n<p>\u89e3\u6790\u5668\u8d1f\u8d23\u8bfb\u53d6XML\u6587\u6863,\u5e76\u5411\u4e8b\u4ef6\u5904\u7406\u5668\u53d1\u9001\u4e8b\u4ef6,\u5982\u5143\u7d20\u5f00\u59cb\u8ddf\u5143\u7d20\u7ed3\u675f\u4e8b\u4ef6;<\/p>\n<p>\u800c\u4e8b\u4ef6\u5904\u7406\u5668\u5219\u8d1f\u8d23\u5bf9\u4e8b\u4ef6\u4f5c\u51fa\u76f8\u5e94,\u5bf9\u4f20\u9012\u7684XML\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<ul>\n<li>\n<p>1\u3001\u5bf9\u5927\u578b\u6587\u4ef6\u8fdb\u884c\u5904\u7406\uff1b<\/p>\n<\/li>\n<li>\n<p>2\u3001\u53ea\u9700\u8981\u6587\u4ef6\u7684\u90e8\u5206\u5185\u5bb9\uff0c\u6216\u8005\u53ea\u9700\u4ece\u6587\u4ef6\u4e2d\u5f97\u5230\u7279\u5b9a\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>3\u3001\u60f3\u5efa\u7acb\u81ea\u5df1\u7684\u6a21\u578b\u7684\u65f6\u5019\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5728python\u4e2d\u4f7f\u7528sax\u65b9\u5f0f\u5904\u7406xml\u8981\u5148\u5f15\u5165xml.sax\u4e2d\u7684parse\u51fd\u6570\uff0c\u8fd8\u6709xml.sax.handler\u4e2d\u7684ContentHandler\u3002<\/p>\n<h3>ContentHandler\u7c7b\u65b9\u6cd5\u4ecb\u7ecd<\/h3>\n<p><strong>characters(content)\u65b9\u6cd5<\/strong><\/p>\n<p>\u8c03\u7528\u65f6\u673a\uff1a<\/p>\n<p>\u4ece\u884c\u5f00\u59cb\uff0c\u9047\u5230\u6807\u7b7e\u4e4b\u524d\uff0c\u5b58\u5728\u5b57\u7b26\uff0ccontent\u7684\u503c\u4e3a\u8fd9\u4e9b\u3002<\/p>\n<p>\u4ece\u4e00\u4e2a\u6807\u7b7e\uff0c\u9047\u5230\u4e0b\u4e00\u4e2a\u6807\u7b7e\u4e4b\u524d\uff0c \u5b58\u5728\u5b57\u7b26\uff0ccontent\u7684\u503c\u4e3a\u8fd9\u4e9b\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ece\u4e00\u4e2a\u6807\u7b7e\uff0c\u9047\u5230\u884c\u7ed3\u675f\u7b26\u4e4b\u524d\uff0c\u5b58\u5728\u5b57\u7b26\uff0ccontent\u7684\u503c\u4e3a\u8fd9\u4e9b\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u6807\u7b7e\u53ef\u4ee5\u662f\u5f00\u59cb\u6807\u7b7e\uff0c\u4e5f\u53ef\u4ee5\u662f\u7ed3\u675f\u6807\u7b7e\u3002<\/p>\n<p><strong>startDocument()\u65b9\u6cd5<\/strong><\/p>\n<p>\u6587\u6863\u542f\u52a8\u7684\u65f6\u5019\u8c03\u7528\u3002<\/p>\n<p><strong>Document()\u65b9\u6cd5<\/strong><\/p>\n<p>\u89e3\u6790\u5668\u5230\u8fbe\u6587\u6863\u7ed3\u5c3e\u65f6\u8c03\u7528\u3002<\/p>\n<p><strong>startElement(name, attrs)\u65b9\u6cd5<\/strong><\/p>\n<p>\u9047\u5230XML\u5f00\u59cb\u6807\u7b7e\u65f6\u8c03\u7528\uff0cname\u662f\u6807\u7b7e\u7684\u540d\u5b57\uff0cattrs\u662f\u6807\u7b7e\u7684\u503c\u5b57\u5178\u3002<\/p>\n<p><strong>endElement(name)\u65b9\u6cd5<\/strong><\/p>\n<p>\u9047\u5230XML\u7ed3\u675f\u6807\u7b7e\u65f6\u8c03\u7528\u3002<\/p>\n<hr>\n<h2>make_parser\u65b9\u6cd5<\/h2>\n<p>\u4ee5\u4e0b\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u89e3\u6790\u5668\u5bf9\u8c61\u5e76\u8fd4\u56de\u3002<\/p>\n<pre>xml.sax.make_parser(&nbsp;[parser_list]&nbsp;)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53c2\u6570\u8bf4\u660e:<\/p>\n<ul>\n<li>\n<p><strong>parser_list<\/strong> &#8211; \u53ef\u9009\u53c2\u6570\uff0c\u89e3\u6790\u5668\u5217\u8868<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2>parser\u65b9\u6cd5<\/h2>\n<p>\u4ee5\u4e0b\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a SAX \u89e3\u6790\u5668\u5e76\u89e3\u6790xml\u6587\u6863\uff1a<\/p>\n<pre>xml.sax.parse(&nbsp;xmlfile,&nbsp;contenthandler[,&nbsp;errorhandler])<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53c2\u6570\u8bf4\u660e:<\/p>\n<ul>\n<li>\n<p><strong>xmlfile<\/strong> &#8211; xml\u6587\u4ef6\u540d<\/p>\n<\/li>\n<li>\n<p><strong>contenthandler<\/strong> &#8211; \u5fc5\u987b\u662f\u4e00\u4e2aContentHandler\u7684\u5bf9\u8c61<\/p>\n<\/li>\n<li>\n<p><strong>errorhandler<\/strong> &#8211; \u5982\u679c\u6307\u5b9a\u8be5\u53c2\u6570\uff0cerrorhandler\u5fc5\u987b\u662f\u4e00\u4e2aSAX ErrorHandler\u5bf9\u8c61<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2>parse\u65b9\u6cd5<\/h2>\n<p>parseString\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2aXML\u89e3\u6790\u5668\u5e76\u89e3\u6790xml\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>xml.sax.parseString(xmlstring,&nbsp;contenthandler[,&nbsp;errorhandler])<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53c2\u6570\u8bf4\u660e:<\/p>\n<ul>\n<li>\n<p><strong>xmlstring<\/strong> &#8211; xml\u5b57\u7b26\u4e32<\/p>\n<\/li>\n<li>\n<p><strong>contenthandler<\/strong> &#8211; \u5fc5\u987b\u662f\u4e00\u4e2aContentHandler\u7684\u5bf9\u8c61<\/p>\n<\/li>\n<li>\n<p><strong>errorhandler<\/strong> &#8211; \u5982\u679c\u6307\u5b9a\u8be5\u53c2\u6570\uff0cerrorhandler\u5fc5\u987b\u662f\u4e00\u4e2aSAX ErrorHandler\u5bf9\u8c61<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2>Python \u89e3\u6790XML\u5b9e\u4f8b<\/h2>\n<pre>#!\/usr\/bin\/python#&nbsp;-*-&nbsp;coding:&nbsp;UTF-8&nbsp;-*-import&nbsp;xml.saxclass&nbsp;MovieHandler(&nbsp;xml.sax.ContentHandler&nbsp;):\r\n&nbsp;&nbsp;&nbsp;def&nbsp;init(self):\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.CurrentData&nbsp;=&nbsp;\"\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.type&nbsp;=&nbsp;\"\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.format&nbsp;=&nbsp;\"\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.year&nbsp;=&nbsp;\"\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.rating&nbsp;=&nbsp;\"\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.stars&nbsp;=&nbsp;\"\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.description&nbsp;=&nbsp;\"\"\r\n\r\n&nbsp;&nbsp;&nbsp;#&nbsp;\u5143\u7d20\u5f00\u59cb\u4e8b\u4ef6\u5904\u7406\r\n&nbsp;&nbsp;&nbsp;def&nbsp;startElement(self,&nbsp;tag,&nbsp;attributes):\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.CurrentData&nbsp;=&nbsp;tag&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;tag&nbsp;==&nbsp;\"movie\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"*****Movie*****\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;title&nbsp;=&nbsp;attributes[\"title\"]\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"Title:\",&nbsp;title&nbsp;&nbsp;&nbsp;#&nbsp;\u5143\u7d20\u7ed3\u675f\u4e8b\u4ef6\u5904\u7406\r\n&nbsp;&nbsp;&nbsp;def&nbsp;endElement(self,&nbsp;tag):\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;self.CurrentData&nbsp;==&nbsp;\"type\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"Type:\",&nbsp;self.type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"format\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"Format:\",&nbsp;self.format&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"year\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"Year:\",&nbsp;self.year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"rating\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"Rating:\",&nbsp;self.rating\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"stars\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"Stars:\",&nbsp;self.stars\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"description\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"Description:\",&nbsp;self.description\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.CurrentData&nbsp;=&nbsp;\"\"\r\n\r\n&nbsp;&nbsp;&nbsp;#&nbsp;\u5185\u5bb9\u4e8b\u4ef6\u5904\u7406\r\n&nbsp;&nbsp;&nbsp;def&nbsp;characters(self,&nbsp;content):\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;self.CurrentData&nbsp;==&nbsp;\"type\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.type&nbsp;=&nbsp;content\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"format\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.format&nbsp;=&nbsp;content\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"year\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.year&nbsp;=&nbsp;content\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"rating\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.rating&nbsp;=&nbsp;content\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"stars\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.stars&nbsp;=&nbsp;content\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;self.CurrentData&nbsp;==&nbsp;\"description\":\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.description&nbsp;=&nbsp;content\r\n&nbsp;&nbsp;\r\nif&nbsp;(&nbsp;name&nbsp;==&nbsp;\"main\"):\r\n&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;#&nbsp;\u521b\u5efa\u4e00\u4e2a&nbsp;XMLReader\r\n&nbsp;&nbsp;&nbsp;parser&nbsp;=&nbsp;xml.sax.make_parser()\r\n&nbsp;&nbsp;&nbsp;#&nbsp;turn&nbsp;off&nbsp;namepsaces\r\n&nbsp;&nbsp;&nbsp;parser.setFeature(xml.sax.handler.feature_namespaces,&nbsp;0)\r\n\r\n&nbsp;&nbsp;&nbsp;#&nbsp;\u91cd\u5199&nbsp;ContextHandler\r\n&nbsp;&nbsp;&nbsp;Handler&nbsp;=&nbsp;MovieHandler()\r\n&nbsp;&nbsp;&nbsp;parser.setContentHandler(&nbsp;Handler&nbsp;)\r\n&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;parser.parse(\"movies.xml\")<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>*****Movie*****Title:&nbsp;Enemy&nbsp;BehindType:&nbsp;War,&nbsp;ThrillerFormat:&nbsp;DVDYear:&nbsp;2003Rating:&nbsp;PGStars:&nbsp;10Description:&nbsp;Talk&nbsp;about&nbsp;a&nbsp;US-Japan&nbsp;war*****Movie*****Title:&nbsp;TransformersType:&nbsp;Anime,&nbsp;Science&nbsp;FictionFormat:&nbsp;DVDYear:&nbsp;1989Rating:&nbsp;RStars:&nbsp;8Description:&nbsp;A&nbsp;schientific&nbsp;fiction*****Movie*****Title:&nbsp;TrigunType:&nbsp;Anime,&nbsp;ActionFormat:&nbsp;DVDRating:&nbsp;PGStars:&nbsp;10Description:&nbsp;Vash&nbsp;the&nbsp;Stampede!*****Movie*****Title:&nbsp;IshtarType:&nbsp;ComedyFormat:&nbsp;VHSRating:&nbsp;PGStars:&nbsp;2Description:&nbsp;Viewable&nbsp;boredom<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h2>\u4f7f\u7528xml.dom\u89e3\u6790xml<\/h2>\n<p>\u6587\u4ef6\u5bf9\u8c61\u6a21\u578b\uff08Document Object Model\uff0c\u7b80\u79f0DOM\uff09\uff0c\u662f\u7ec4\u7ec7\u63a8\u8350\u7684\u5904\u7406\u53ef\u6269\u5c55\u7f6e\u6807\u8bed\u8a00\u7684\u6807\u51c6\u7f16\u7a0b\u63a5\u53e3\u3002<\/p>\n<p>\u4e00\u4e2a DOM \u7684\u89e3\u6790\u5668\u5728\u89e3\u6790\u4e00\u4e2a XML \u6587\u6863\u65f6\uff0c\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u6863\uff0c\u628a\u6587\u6863\u4e2d\u6240\u6709\u5143\u7d20\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u7684\u4e00\u4e2a\u6811\u7ed3\u6784\u91cc\uff0c\u4e4b\u540e\u4f60\u53ef\u4ee5\u5229\u7528DOM \u63d0\u4f9b\u7684\u4e0d\u540c\u7684\u51fd\u6570\u6765\u8bfb\u53d6\u6216\u4fee\u6539\u6587\u6863\u7684\u5185\u5bb9\u548c\u7ed3\u6784\uff0c\u4e5f\u53ef\u4ee5\u628a\u4fee\u6539\u8fc7\u7684\u5185\u5bb9\u5199\u5165xml\u6587\u4ef6\u3002<\/p>\n<p>python\u4e2d\u7528xml.dom.minidom\u6765\u89e3\u6790xml\u6587\u4ef6\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>#!\/usr\/bin\/python#&nbsp;-*-&nbsp;coding:&nbsp;UTF-8&nbsp;-*-from&nbsp;xml.dom.minidom&nbsp;import&nbsp;parseimport&nbsp;xml.dom.minidom#&nbsp;\u4f7f\u7528minidom\u89e3\u6790\u5668\u6253\u5f00&nbsp;XML&nbsp;\u6587\u6863DOMTree&nbsp;=&nbsp;xml.dom.minidom.parse(\"movies.xml\")collection&nbsp;=&nbsp;DOMTree.documentElementif&nbsp;collection.hasAttribute(\"shelf\"):\r\n&nbsp;&nbsp;&nbsp;print&nbsp;\"Root&nbsp;element&nbsp;:&nbsp;%s\"&nbsp;%&nbsp;collection.getAttribute(\"shelf\")#&nbsp;\u5728\u96c6\u5408\u4e2d\u83b7\u53d6\u6240\u6709\u7535\u5f71movies&nbsp;=&nbsp;collection.getElementsByTagName(\"movie\")#&nbsp;\u6253\u5370\u6bcf\u90e8\u7535\u5f71\u7684\u8be6\u7ec6\u4fe1\u606ffor&nbsp;movie&nbsp;in&nbsp;movies:\r\n&nbsp;&nbsp;&nbsp;print&nbsp;\"*****Movie*****\"\r\n&nbsp;&nbsp;&nbsp;if&nbsp;movie.hasAttribute(\"title\"):\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;\"Title:&nbsp;%s\"&nbsp;%&nbsp;movie.getAttribute(\"title\")\r\n\r\n&nbsp;&nbsp;&nbsp;type&nbsp;=&nbsp;movie.getElementsByTagName('type')[0]\r\n&nbsp;&nbsp;&nbsp;print&nbsp;\"Type:&nbsp;%s\"&nbsp;%&nbsp;type.childNodes[0].data\r\n&nbsp;&nbsp;&nbsp;format&nbsp;=&nbsp;movie.getElementsByTagName('format')[0]\r\n&nbsp;&nbsp;&nbsp;print&nbsp;\"Format:&nbsp;%s\"&nbsp;%&nbsp;format.childNodes[0].data\r\n&nbsp;&nbsp;&nbsp;rating&nbsp;=&nbsp;movie.getElementsByTagName('rating')[0]\r\n&nbsp;&nbsp;&nbsp;print&nbsp;\"Rating:&nbsp;%s\"&nbsp;%&nbsp;rating.childNodes[0].data\r\n&nbsp;&nbsp;&nbsp;description&nbsp;=&nbsp;movie.getElementsByTagName('description')[0]\r\n&nbsp;&nbsp;&nbsp;print&nbsp;\"Description:&nbsp;%s\"&nbsp;%&nbsp;description.childNodes[0].data<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>Root&nbsp;element&nbsp;:&nbsp;New&nbsp;Arrivals*****Movie*****Title:&nbsp;Enemy&nbsp;BehindType:&nbsp;War,&nbsp;ThrillerFormat:&nbsp;DVDRating:&nbsp;PGDescription:&nbsp;Talk&nbsp;about&nbsp;a&nbsp;US-Japan&nbsp;war*****Movie*****Title:&nbsp;TransformersType:&nbsp;Anime,&nbsp;Science&nbsp;FictionFormat:&nbsp;DVDRating:&nbsp;RDescription:&nbsp;A&nbsp;schientific&nbsp;fiction*****Movie*****Title:&nbsp;TrigunType:&nbsp;Anime,&nbsp;ActionFormat:&nbsp;DVDRating:&nbsp;PGDescription:&nbsp;Vash&nbsp;the&nbsp;Stampede!*****Movie*****Title:&nbsp;IshtarType:&nbsp;ComedyFormat:&nbsp;VHSRating:&nbsp;PGDescription:&nbsp;Viewable&nbsp;boredom<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPython\u4e4bXML\u89e3\u6790\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\uff1f xml \u6307\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\uff08extensible markup language\uff09\u3002 XML \u88ab\u8bbe\u8ba1\u7528\u6765\u4f20\u8f93\u548c\u5b58\u50a8\u6570\u636e\u3002 XML\u662f\u4e00\u5957\u5b9a\u4e49\u8bed\u4e49\u6807\u8bb0\u7684\u89c4\u5219\uff0c\u8fd9\u4e9b\u6807\u8bb0\u5c06\u6587\u6863\u5206\u6210\u8bb8\u591a\u90e8\u4ef6\u5e76\u5bf9\u8fd9\u4e9b\u90e8\u4ef6\u52a0\u4ee5\u6807\u8bc6\u3002 \u5b83\u4e5f\u662f\u5143\u6807\u8bb0\u8bed\u8a00\uff0c\u5373\u5b9a\u4e49\u4e86\u7528\u4e8e\u5b9a\u4e49\u5176\u4ed6\u4e0e\u7279\u5b9a\u9886\u57df\u6709\u5173\u7684\u3001\u8bed\u4e49\u7684\u3001\u7ed3\u6784\u5316\u7684\u6807\u8bb0\u8bed\u8a00\u7684\u53e5\u6cd5\u8bed\u8a00\u3002 \u5bf9XML\u7684\u89e3\u6790 \u5e38\u89c1\u7684XML\u6709DOM\u548cSAX\uff0c\u8fd9\u4e24\u79cd\u63a5\u53e3\u5904\u7406XML\u6587\u4ef6\u7684\u65b9\u5f0f\u4e0d\u540c\uff0c\u5f53\u7136\u4f7f\u7528\u573a\u5408\u4e5f\u4e0d\u540c\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b python\u6709\u4e09\u79cd\u65b9\u6cd5\u89e3\u6790XML\uff0cSAX\uff0cDOM\uff0c\u4ee5\u53caElementTree: 1.SAX (simple XML ) python \u6807\u51c6\u5e93\u5305\u542bSAX\u89e3\u6790\u5668\uff0cSAX\u7528\uff0c\u901a\u8fc7\u5728\u89e3\u6790XML\u7684\u8fc7\u7a0b\u4e2d\u89e6\u53d1\u4e00\u4e2a\u4e2a\u7684\u4e8b\u4ef6\u5e76\u8c03\u7528\u7528\u6237\u5b9a\u4e49\u7684\u6765\u5904\u7406XML\u6587\u4ef6\u3002 2.DOM( ) \u5c06XML\u6570\u636e\u5728\u5185\u5b58\u4e2d\u89e3\u6790\u6210\u4e00\u4e2a\u6811\uff0c\u901a\u8fc7\u5bf9\u6811\u7684\u64cd\u4f5c\u6765\u64cd\u4f5cXML\u3002 3.ElementTree(\u5143\u7d20\u6811) ElementTree\u5c31\u50cf\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684DOM\uff0c\u5177\u6709\u65b9\u4fbf\u53cb\u597d\u7684API\u3002\u4ee3\u7801\u53ef\u7528\u6027\u597d\uff0c\u901f\u5ea6\u5feb\uff0c\u6d88\u8017\u5185\u5b58\u5c11\u3002 \u6ce8\uff1a\u56e0DOM\u9700\u8981\u5c06XML\u6570\u636e\u6620\u5c04\u5230\u5185\u5b58\u4e2d\u7684\u6811\uff0c\u4e00\u662f\u6bd4\u8f83\u6162\uff0c\u4e8c\u662f\u6bd4\u8f83\u8017\u5185\u5b58\uff0c\u800cSAX\u6d41\u5f0f\u8bfb\u53d6XML\u6587\u4ef6\uff0c\u6bd4\u8f83\u5feb\uff0c\u5360\u7528\u5185\u5b58\u5c11\uff0c\u4f46\u9700\u8981\u7528\u6237\u5b9e\u73b0\u56de\u8c03\u51fd\u6570\uff08haner\uff09\u3002 \u672c\u7ae0\u8282\u4f7f\u7528\u5230\u7684XML\u5b9e\u4f8b\u6587\u4ef6movies.xml\u5185\u5bb9\u5982\u4e0b\uff1a &lt;collection&gt;&lt;movie&gt; &nbsp;&nbsp;&nbsp;&lt;type&gt;War,&nbsp;Thriller&lt;\/type&gt; &nbsp;&nbsp;&nbsp;&lt;format&gt;DVD&lt;\/format&gt; &nbsp;&nbsp;&nbsp;&lt;year&gt;2003&lt;\/year&gt; &nbsp;&nbsp;&nbsp;&lt;rating&gt;PG&lt;\/rating&gt; &nbsp;&nbsp;&nbsp;&lt;stars&gt;10&lt;\/stars&gt; &nbsp;&nbsp;&nbsp;&lt;description&gt;Talk&nbsp;about&nbsp;a&nbsp;US-Japan&nbsp;war&lt;\/description&gt;&lt;\/movie&gt;&lt;movie&gt; &nbsp;&nbsp;&nbsp;&lt;type&gt;Anime,&nbsp;Science&nbsp;Fiction&lt;\/type&gt; &nbsp;&nbsp;&nbsp;&lt;format&gt;DVD&lt;\/format&gt; &nbsp;&nbsp;&nbsp;&lt;year&gt;1989&lt;\/year&gt; &nbsp;&nbsp;&nbsp;&lt;rating&gt;R&lt;\/rating&gt; &nbsp;&nbsp;&nbsp;&lt;stars&gt;8&lt;\/stars&gt; &nbsp;&nbsp;&nbsp;&lt;description&gt;A&nbsp;schientific&nbsp;fiction&lt;\/description&gt;&lt;\/movie&gt; &nbsp;&nbsp;&nbsp;&lt;movie&gt; &nbsp;&nbsp;&nbsp;&lt;type&gt;Anime,&nbsp;Action&lt;\/type&gt; &nbsp;&nbsp;&nbsp;&lt;format&gt;DVD&lt;\/format&gt; &nbsp;&nbsp;&nbsp;&lt;episodes&gt;4&lt;\/episodes&gt; &nbsp;&nbsp;&nbsp;&lt;rating&gt;PG&lt;\/rating&gt; &nbsp;&nbsp;&nbsp;&lt;stars&gt;10&lt;\/stars&gt; &nbsp;&nbsp;&nbsp;&lt;description&gt;Vash&nbsp;the&nbsp;Stampede!&lt;\/description&gt;&lt;\/movie&gt;&lt;movie&gt; &nbsp;&nbsp;&nbsp;&lt;type&gt;Comedy&lt;\/type&gt; &nbsp;&nbsp;&nbsp;&lt;format&gt;VHS&lt;\/format&gt; &nbsp;&nbsp;&nbsp;&lt;rating&gt;PG&lt;\/rating&gt; &nbsp;&nbsp;&nbsp;&lt;stars&gt;2&lt;\/stars&gt; &nbsp;&nbsp;&nbsp;&lt;description&gt;Viewable&nbsp;boredom&lt;\/description&gt;&lt;\/movie&gt;&lt;\/collection&gt; \u767b\u5f55\u540e\u590d\u5236 python\u4f7f\u7528SAX\u89e3\u6790xml SAX\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684API\u3002 \u5229\u7528SAX\u89e3\u6790XML\u6587\u6863\u7275\u6d89\u5230\u4e24\u4e2a\u90e8\u5206:\u89e3\u6790\u5668\u548c\u5668\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-28545","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=28545"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28545\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}