{"id":248864,"date":"2022-08-10T10:11:01","date_gmt":"2022-08-10T02:11:01","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=248864"},"modified":"2022-07-29T14:11:41","modified_gmt":"2022-07-29T06:11:41","slug":"python-xml-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-xml-linux.html","title":{"rendered":"python\u5b9e\u73b0\u4fee\u6539xml\u6587\u4ef6\u5185\u5bb9\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86python\u5b9e\u73b0\u4fee\u6539xml\u6587\u4ef6\u5185\u5bb9\uff0cXML \u6307\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\uff0c\u662f\u4e00\u79cd\u6807\u8bb0\u8bed\u8a00\uff0c\u662f\u4ece\u6807\u51c6\u901a\u7528\u6807\u8bb0\u8bed\u8a00\uff08SGML\uff09\u4e2d\u7b80\u5316\u4fee\u6539\u51fa\u6765\u7684<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

XML \u88ab\u8bbe\u8ba1\u7528\u6765\u4f20\u8f93\u548c\u5b58\u50a8\u6570\u636e\u3002<\/p>\n

HTML \u88ab\u8bbe\u8ba1\u7528\u6765\u663e\u793a\u6570\u636e\u3002<\/p>\n

XML \u6307\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\uff08eXtensible Markup Language\uff09\u3002<\/p>\n

\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\uff08\u82f1\u8bed\uff1aExtensible Markup Language\uff0c\u7b80\u79f0\uff1aXML\uff09\u662f\u4e00\u79cd\u6807\u8bb0\u8bed\u8a00\uff0c\u662f\u4ece\u6807\u51c6\u901a\u7528\u6807\u8bb0\u8bed\u8a00\uff08SGML\uff09\u4e2d\u7b80\u5316\u4fee\u6539\u51fa\u6765\u7684\u3002\u5b83\u4e3b\u8981\u7528\u5230\u7684\u6709\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\u3001\u53ef\u6269\u5c55\u6837\u5f0f\u8bed\u8a00\uff08XSL\uff09\u3001XBRL\u548cXPath\u7b49\u3002<\/p>\n

\u76f4\u63a5\u4e0a\u4ee3\u7801\uff0c\u62ff\u6765\u5c31\u53ef\u7528\u3002<\/strong><\/div>\n

\u9996\u5148\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u6d4b\u8bd5\u200b\u200bxml\u200b\u200b\u200b\u6587\u4ef6\uff0c\u6211\u8fd9\u4e2a\u6587\u4ef6\u540d\u5b57\u4e3a\u200b\u200btext.xml\u200b\u200b\uff1b<\/p>\n

\r\n< data>\r\n< country name=\"Liechtenstein\">\r\n< rank>yunweijia< \/rank>\r\n< year>2022< \/year>\r\n< gdppc>141100< \/gdppc>\r\n< neighbor name=\"Austria\" direction=\"E\" \/>\r\n< neighbor name=\"Switzerland\" direction=\"W\" \/>\r\n< \/country>\r\n< country name=\"Singapore\">\r\n< rank>yunweijia< \/rank>\r\n< year>2023< \/year>\r\n< gdppc>59900< \/gdppc>\r\n< neighbor name=\"Malaysia\" direction=\"N\" \/>\r\n< \/country>\r\n< country name=\"Panama\">\r\n< rank>yunweijia< \/rank>\r\n< year>2024< \/year>\r\n< gdppc>13600< \/gdppc>\r\n< neighbor name=\"Costa Rica\" direction=\"W\" \/>\r\n< neighbor name=\"Colombia\" direction=\"E\" \/>\r\n< \/country>\r\n< \/data><\/pre>\n

\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8fdb\u884c\u4fee\u6539\uff1b<\/p>\n

import xml.etree.ElementTree as ET\r\ndef change_one_xml(xml_path, xml_dw, update_content):\r\n# \u6253\u5f00xml\u6587\u6863\r\ndoc = ET.parse(xml_path)\r\nroot = doc.getroot()\r\n# \u67e5\u627e\u4fee\u6539\u8def\u52b2\r\nsub1 = root.find(xml_dw)\r\n# \u4fee\u6539\u6807\u7b7e\u5185\u5bb9\r\nsub1.text = update_content\r\n# \u4fdd\u5b58\u4fee\u6539\r\ndoc.write(xml_path)\r\n \r\n# \u6b32\u4fee\u6539\u6587\u4ef6\r\nxml_path = r'test.xml'\r\n \r\n# \u4fee\u6539\u6587\u4ef6\u4e2d\u7684xpath\u5b9a\u4f4d\r\nxml_dw = '.\/\/country[@name=\"Singapore\"]\/year'\r\n \r\n# \u60f3\u8981\u4fee\u6539\u6210\u4ec0\u4e48\u5185\u5bb9\r\nupdate_content = '9999'\r\nchange_one_xml(xml_path, xml_dw, update_content)<\/pre>\n

\u8fd0\u884c\u5b8c\u6bd5\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6e90\u6587\u4ef6\u5185\u5bb9\u53d8\u6210\u4e86\uff1b<\/p>\n

< data>\r\n< country name=\"Liechtenstein\">\r\n< rank>yunweijia< \/rank>\r\n< year>2022< \/year>\r\n< gdppc>141100< \/gdppc>\r\n< neighbor name=\"Austria\" direction=\"E\" \/>\r\n< neighbor name=\"Switzerland\" direction=\"W\" \/>\r\n< \/country>\r\n< country name=\"Singapore\">\r\n< rank>yunweijia< \/rank>\r\n< year>9999< \/year>\r\n< gdppc>59900< \/gdppc>\r\n< neighbor name=\"Malaysia\" direction=\"N\" \/>\r\n< \/country>\r\n< country name=\"Panama\">\r\n< rank>yunweijia< \/rank>\r\n< year>2024< \/year>\r\n< gdppc>13600< \/gdppc>\r\n< neighbor name=\"Costa Rica\" direction=\"W\" \/>\r\n< neighbor name=\"Colombia\" direction=\"E\" \/>\r\n< \/country>\r\n< \/data><\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8epython\u5b9e\u73b0\u4fee\u6539xml\u6587\u4ef6\u5185\u5bb9\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

XML \u88ab\u8bbe\u8ba1\u7528\u6765\u4f20\u8f93\u548c\u5b58\u50a8\u6570\u636e\u3002 HTML \u88ab\u8bbe\u8ba1\u7528\u6765\u663e\u793a\u6570\u636e\u3002 XML \u6307\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\uff08eXtensib […]<\/p>\n","protected":false},"author":362,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-248864","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\/248864","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\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=248864"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/248864\/revisions"}],"predecessor-version":[{"id":248871,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/248864\/revisions\/248871"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=248864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=248864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=248864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}