{"id":167040,"date":"2019-12-26T12:00:21","date_gmt":"2019-12-26T04:00:21","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=167040"},"modified":"2019-12-17T10:53:27","modified_gmt":"2019-12-17T02:53:27","slug":"python-uses-sax","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-uses-sax.html","title":{"rendered":"Python \u4f7f\u7528 SAX \u89e3\u6790 xml"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | SAX \u662f\u4e00\u79cd\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684API\u3002\u5229\u7528 SAX \u89e3\u6790 XML \u6587\u6863\u7275\u6d89\u5230\u4e24\u4e2a\u90e8\u5206: \u89e3\u6790\u5668\u548c\u4e8b\u4ef6\u5904\u7406\u5668\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n \u89e3\u6790\u5668\u8d1f\u8d23\u8bfb\u53d6 XML \u6587\u6863\uff0c\u5e76\u5411\u4e8b\u4ef6\u5904\u7406\u5668\u53d1\u9001\u4e8b\u4ef6\uff0c\u5982\u5143\u7d20\u5f00\u59cb\u8ddf\u5143\u7d20\u7ed3\u675f\u4e8b\u4ef6\u3002<\/p>\n \u800c\u4e8b\u4ef6\u5904\u7406\u5668\u5219\u8d1f\u8d23\u5bf9\u4e8b\u4ef6\u4f5c\u51fa\u54cd\u5e94\uff0c\u5bf9\u4f20\u9012\u7684 XML \u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n 1\u3001\u5bf9\u5927\u578b\u6587\u4ef6\u8fdb\u884c\u5904\u7406\uff1b<\/p>\n 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 3\u3001\u60f3\u5efa\u7acb\u81ea\u5df1\u7684\u5bf9\u8c61\u6a21\u578b\u7684\u65f6\u5019\u3002<\/p>\n \u5728 Python \u4e2d\u4f7f\u7528 sax \u65b9\u5f0f\u5904\u7406 xml \u8981\u5148\u5f15\u5165 xml.sax \u4e2d\u7684 parse \u51fd\u6570\uff0c\u8fd8\u6709 xml.sax.handler \u4e2d\u7684 ContentHandler\u3002<\/p>\n ContentHandler \u7c7b\u65b9\u6cd5\u4ecb\u7ecd<\/strong><\/div>\n characters(content) \u65b9\u6cd5<\/strong><\/p>\n \u8c03\u7528\u65f6\u673a\uff1a<\/p>\n \u4ece\u884c\u5f00\u59cb\uff0c\u9047\u5230\u6807\u7b7e\u4e4b\u524d\uff0c\u5b58\u5728\u5b57\u7b26\uff0ccontent \u7684\u503c\u4e3a\u8fd9\u4e9b\u5b57\u7b26\u4e32\u3002<\/p>\n \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 \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 \u6807\u7b7e\u53ef\u4ee5\u662f\u5f00\u59cb\u6807\u7b7e\uff0c\u4e5f\u53ef\u4ee5\u662f\u7ed3\u675f\u6807\u7b7e\u3002<\/p>\n startDocument() \u65b9\u6cd5<\/strong><\/p>\n \u6587\u6863\u542f\u52a8\u7684\u65f6\u5019\u8c03\u7528\u3002<\/p>\n endDocument() \u65b9\u6cd5<\/strong><\/p>\n \u89e3\u6790\u5668\u5230\u8fbe\u6587\u6863\u7ed3\u5c3e\u65f6\u8c03\u7528\u3002<\/p>\n startElement(name, attrs) \u65b9\u6cd5<\/strong><\/p>\n \u9047\u5230XML\u5f00\u59cb\u6807\u7b7e\u65f6\u8c03\u7528\uff0cname \u662f\u6807\u7b7e\u7684\u540d\u5b57\uff0cattrs \u662f\u6807\u7b7e\u7684\u5c5e\u6027\u503c\u5b57\u5178\u3002<\/p>\n endElement(name) \u65b9\u6cd5<\/strong><\/p>\n \u9047\u5230XML\u7ed3\u675f\u6807\u7b7e\u65f6\u8c03\u7528\u3002<\/p>\n \u4ee5\u4e0b\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u89e3\u6790\u5668\u5bf9\u8c61\u5e76\u8fd4\u56de\u3002<\/p>\n \u53c2\u6570\u8bf4\u660e:<\/p>\n \u4ee5\u4e0b\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a SAX \u89e3\u6790\u5668\u5e76\u89e3\u6790xml\u6587\u6863\uff1a<\/p>\n xml.sax.parse( xmlfile, contenthandler[, errorhandler])<\/p>\n \u53c2\u6570\u8bf4\u660e:<\/p>\n xmlfile - xml\u6587\u4ef6\u540d<\/p>\n contenthandler - \u5fc5\u987b\u662f\u4e00\u4e2a ContentHandler \u7684\u5bf9\u8c61<\/p>\n errorhandler - \u5982\u679c\u6307\u5b9a\u8be5\u53c2\u6570\uff0cerrorhandler \u5fc5\u987b\u662f\u4e00\u4e2a SAX ErrorHandler \u5bf9\u8c61<\/p>\n parseString \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a XML \u89e3\u6790\u5668\u5e76\u89e3\u6790 xml \u5b57\u7b26\u4e32\uff1a<\/p>\n \u53c2\u6570\u8bf4\u660e:<\/p>\n xmlstring - xml\u5b57\u7b26\u4e32<\/p>\n contenthandler - \u5fc5\u987b\u662f\u4e00\u4e2a ContentHandler \u7684\u5bf9\u8c61<\/p>\n errorhandler - \u5982\u679c\u6307\u5b9a\u8be5\u53c2\u6570\uff0cerrorhandler \u5fc5\u987b\u662f\u4e00\u4e2a SAX ErrorHandler\u5bf9\u8c61<\/p>\n","protected":false},"excerpt":{"rendered":" \u89e3\u6790\u5668\u8d1f\u8d23\u8bfb\u53d6 XML \u6587\u6863\uff0c\u5e76\u5411\u4e8b\u4ef6\u5904\u7406\u5668\u53d1\u9001\u4e8b\u4ef6\uff0c\u5982\u5143\u7d20\u5f00\u59cb\u8ddf\u5143\u7d20\u7ed3\u675f\u4e8b\u4ef6\u3002 \u800c\u4e8b\u4ef6\u5904\u7406\u5668\u5219\u8d1f\u8d23\u5bf9\u4e8b\u4ef6\u4f5c\u51fa […]<\/p>\n","protected":false},"author":1470,"featured_media":167042,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167040","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167040"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167040\/revisions"}],"predecessor-version":[{"id":167045,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167040\/revisions\/167045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167042"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}} |