{"id":1008083,"date":"2024-12-27T10:59:33","date_gmt":"2024-12-27T02:59:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008083.html"},"modified":"2024-12-27T10:59:37","modified_gmt":"2024-12-27T02:59:37","slug":"python%e5%a6%82%e4%bd%95%e5%8f%91%e9%80%81xml%e6%8a%a5%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008083.html","title":{"rendered":"python\u5982\u4f55\u53d1\u9001xml\u62a5\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083532\/ea2f40d3-3f69-42d9-bfc8-c6cca27cc549.webp\" alt=\"python\u5982\u4f55\u53d1\u9001xml\u62a5\u6587\" \/><\/p>\n<p><p> <strong>Python\u53d1\u9001XML\u62a5\u6587\u7684\u8fc7\u7a0b\u4e3b\u8981\u5305\u62ec\uff1a\u6784\u5efaXML\u6570\u636e\u3001\u4f7f\u7528\u8bf7\u6c42\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u5904\u7406\u54cd\u5e94\u3002<\/strong>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2aXML\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u63a5\u7740\u4f7f\u7528Python\u7684requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u6700\u540e\u5904\u7406\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u6b65\u9aa4\uff1a\u6784\u5efaXML\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u6784\u5efaXML\u6570\u636e\uff0c\u5e38\u7528\u7684\u6709xml.etree.ElementTree\u5e93\u548clxml\u5e93\u3002xml.etree.ElementTree\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u89e3\u6790XML\u6587\u6863\uff1b\u800clxml\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528xml.etree.ElementTree\u5e93\u6784\u5efaXML\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<p>root = ET.Element(&quot;note&quot;)<\/p>\n<p>to = ET.SubElement(root, &quot;to&quot;)<\/p>\n<p>to.text = &quot;Tove&quot;<\/p>\n<p>from_ = ET.SubElement(root, &quot;from&quot;)<\/p>\n<p>from_.text = &quot;Jani&quot;<\/p>\n<p>heading = ET.SubElement(root, &quot;heading&quot;)<\/p>\n<p>heading.text = &quot;Reminder&quot;<\/p>\n<p>body = ET.SubElement(root, &quot;body&quot;)<\/p>\n<p>body.text = &quot;Don&#39;t forget me this weekend!&quot;<\/p>\n<p>tree = ET.ElementTree(root)<\/p>\n<p>tree.write(&quot;note.xml&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3Python\u5982\u4f55\u53d1\u9001XML\u62a5\u6587\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6784\u5efaXML\u6570\u636e<\/p>\n<\/p>\n<p><p>\u6784\u5efaXML\u6570\u636e\u662f\u53d1\u9001XML\u62a5\u6587\u7684\u7b2c\u4e00\u6b65\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6784\u5efaXML\u6570\u636e\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528xml.etree.ElementTree\u5e93\u548clxml\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528xml.etree.ElementTree\u5e93<\/li>\n<\/ol>\n<p><p>xml.etree.ElementTree\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u89e3\u6790XML\u6587\u6863\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u76f8\u5bf9\u7b80\u5355\uff0c\u9002\u5408\u5904\u7406\u8f83\u5c0f\u7684XML\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u521b\u5efa\u6839\u5143\u7d20<\/strong><\/h2>\n<p>root = ET.Element(&quot;note&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u8bbe\u7f6e\u6587\u672c<\/strong><\/h2>\n<p>to = ET.SubElement(root, &quot;to&quot;)<\/p>\n<p>to.text = &quot;Tove&quot;<\/p>\n<p>from_ = ET.SubElement(root, &quot;from&quot;)<\/p>\n<p>from_.text = &quot;Jani&quot;<\/p>\n<p>heading = ET.SubElement(root, &quot;heading&quot;)<\/p>\n<p>heading.text = &quot;Reminder&quot;<\/p>\n<p>body = ET.SubElement(root, &quot;body&quot;)<\/p>\n<p>body.text = &quot;Don&#39;t forget me this weekend!&quot;<\/p>\n<h2><strong>\u5c06XML\u6570\u636e\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>tree = ET.ElementTree(root)<\/p>\n<p>tree.write(&quot;note.xml&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528lxml\u5e93<\/li>\n<\/ol>\n<p><p>lxml\u5e93\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684XML\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd\u3002\u5b83\u652f\u6301XPath\u3001XSLT\u7b49\u9ad8\u7ea7\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684XML\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<h2><strong>\u521b\u5efa\u6839\u5143\u7d20<\/strong><\/h2>\n<p>root = etree.Element(&quot;note&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u8bbe\u7f6e\u6587\u672c<\/strong><\/h2>\n<p>to = etree.SubElement(root, &quot;to&quot;)<\/p>\n<p>to.text = &quot;Tove&quot;<\/p>\n<p>from_ = etree.SubElement(root, &quot;from&quot;)<\/p>\n<p>from_.text = &quot;Jani&quot;<\/p>\n<p>heading = etree.SubElement(root, &quot;heading&quot;)<\/p>\n<p>heading.text = &quot;Reminder&quot;<\/p>\n<p>body = etree.SubElement(root, &quot;body&quot;)<\/p>\n<p>body.text = &quot;Don&#39;t forget me this weekend!&quot;<\/p>\n<h2><strong>\u5c06XML\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>xml_data = etree.tostring(root, pretty_print=True, xml_declaration=True, encoding=&#39;UTF-8&#39;)<\/p>\n<h2><strong>\u6253\u5370XML\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>print(xml_data.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u8bf7\u6c42\u5e93\u53d1\u9001HTTP\u8bf7\u6c42<\/p>\n<\/p>\n<p><p>\u6784\u5efa\u597dXML\u6570\u636e\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5c06\u5176\u53d1\u9001\u5230\u670d\u52a1\u5668\u3002Python\u7684requests\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684HTTP\u8bf7\u6c42\u5e93\uff0c\u652f\u6301\u53d1\u9001GET\u3001POST\u7b49\u591a\u79cd\u7c7b\u578b\u7684\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5requests\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528requests\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u53d1\u9001POST\u8bf7\u6c42<\/li>\n<\/ol>\n<p><p>\u5728\u53d1\u9001POST\u8bf7\u6c42\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528requests.post()\u65b9\u6cd5\u3002\u9700\u8981\u5c06XML\u6570\u636e\u4f5c\u4e3a\u8bf7\u6c42\u4f53\u53d1\u9001\uff0c\u5e76\u8bbe\u7f6e\u9002\u5f53\u7684\u8bf7\u6c42\u5934\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u76ee\u6807URL<\/strong><\/h2>\n<p>url = &quot;http:\/\/example.com\/api&quot;<\/p>\n<h2><strong>\u8bf7\u6c42\u5934<\/strong><\/h2>\n<p>headers = {<\/p>\n<p>    &#39;Content-Type&#39;: &#39;application\/xml&#39;,<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001POST\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.post(url, data=xml_data, headers=headers)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u72b6\u6001\u7801\u548c\u5185\u5bb9<\/strong><\/h2>\n<p>print(response.status_code)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u54cd\u5e94<\/p>\n<\/p>\n<p><p>\u53d1\u9001\u8bf7\u6c42\u540e\uff0c\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u54cd\u5e94\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7requests\u5e93\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u5904\u7406\u54cd\u5e94\u3002<\/p>\n<\/p>\n<ol>\n<li>\u68c0\u67e5\u54cd\u5e94\u72b6\u6001\u7801<\/li>\n<\/ol>\n<p><p>response\u5bf9\u8c61\u7684status_code\u5c5e\u6027\u5305\u542b\u4e86HTTP\u54cd\u5e94\u7684\u72b6\u6001\u7801\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if response.status_code == 200:<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a{response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9<\/li>\n<\/ol>\n<p><p>response\u5bf9\u8c61\u7684text\u5c5e\u6027\u5305\u542b\u4e86\u54cd\u5e94\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">response_data = response.text<\/p>\n<p>print(response_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u89e3\u6790XML\u54cd\u5e94<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u670d\u52a1\u5668\u8fd4\u56de\u7684\u662fXML\u683c\u5f0f\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528xml.etree.ElementTree\u5e93\u6216lxml\u5e93\u6765\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u89e3\u6790XML\u54cd\u5e94<\/strong><\/h2>\n<p>root = ET.fromstring(response_data)<\/p>\n<h2><strong>\u8bbf\u95eeXML\u5143\u7d20<\/strong><\/h2>\n<p>for child in root:<\/p>\n<p>    print(child.tag, child.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u53d1\u9001XML\u62a5\u6587\u6d89\u53ca\u5230\u4e09\u4e2a\u4e3b\u8981\u6b65\u9aa4\uff1a\u6784\u5efaXML\u6570\u636e\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u5904\u7406\u54cd\u5e94\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u5e93\u6765\u5b8c\u6210\u8fd9\u4e9b\u4efb\u52a1\uff0c\u5982xml.etree.ElementTree\u3001lxml\u548crequests\u5e93\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u53d1\u9001XML\u62a5\u6587\u7684\u529f\u80fd\u3002\u901a\u8fc7\u4ee5\u4e0a\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u4f7f\u7528Python\u53d1\u9001XML\u62a5\u6587\u7684\u57fa\u672c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6784\u5efaXML\u62a5\u6587\uff1f<\/strong><br \/>\u5728Python\u4e2d\u6784\u5efaXML\u62a5\u6587\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u6a21\u5757\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u6839\u5143\u7d20\uff0c\u6dfb\u52a0\u5b50\u5143\u7d20\uff0c\u5e76\u4f7f\u7528<code>ElementTree<\/code>\u7684<code>toxml()<\/code>\u6216<code>write()<\/code>\u65b9\u6cd5\u6765\u751f\u6210\u6700\u7ec8\u7684XML\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-python\">import xml.etree.ElementTree as ET\n\nroot = ET.Element(&quot;Message&quot;)\nchild = ET.SubElement(root, &quot;Data&quot;)\nchild.text = &quot;Hello, World!&quot;\n\ntree = ET.ElementTree(root)\nET.dump(tree)  # \u8f93\u51fa\u5230\u63a7\u5236\u53f0\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u53d1\u9001XML\u62a5\u6587\u5230\u670d\u52a1\u5668\uff1f<\/strong><br \/>\u8981\u5c06XML\u62a5\u6587\u53d1\u9001\u5230\u670d\u52a1\u5668\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u3002\u6784\u5efa\u597dXML\u5b57\u7b26\u4e32\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>requests.post()<\/code>\u65b9\u6cd5\u5c06\u5176\u4f5c\u4e3a\u8bf7\u6c42\u4f53\u53d1\u9001\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nxml_data = &#39;&#39;&#39;&lt;Message&gt;&lt;Data&gt;Hello, World!&lt;\/Data&gt;&lt;\/Message&gt;&#39;&#39;&#39;\nresponse = requests.post(&#39;http:\/\/your-api-endpoint.com&#39;, data=xml_data, headers={&#39;Content-Type&#39;: &#39;application\/xml&#39;})\n\nprint(response.status_code)\nprint(response.text)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u670d\u52a1\u5668\u8fd4\u56de\u7684XML\u54cd\u5e94\uff1f<\/strong><br \/>\u5904\u7406\u670d\u52a1\u5668\u8fd4\u56de\u7684XML\u54cd\u5e94\u53ef\u4ee5\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u6a21\u5757\u3002\u6536\u5230\u54cd\u5e94\u540e\uff0c\u53ef\u4ee5\u89e3\u6790XML\u5185\u5bb9\u5e76\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">response_xml = response.content\nroot = ET.fromstring(response_xml)\n\nfor child in root:\n    print(child.tag, child.text)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u53d1\u9001XML\u62a5\u6587\u5e76\u5904\u7406\u54cd\u5e94\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53d1\u9001XML\u62a5\u6587\u7684\u8fc7\u7a0b\u4e3b\u8981\u5305\u62ec\uff1a\u6784\u5efaXML\u6570\u636e\u3001\u4f7f\u7528\u8bf7\u6c42\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u5904\u7406\u54cd\u5e94\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981 [&hellip;]","protected":false},"author":3,"featured_media":1008092,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008083"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1008083"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008083\/revisions"}],"predecessor-version":[{"id":1008094,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008083\/revisions\/1008094"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008092"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}