{"id":1094261,"date":"2025-01-08T14:39:22","date_gmt":"2025-01-08T06:39:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094261.html"},"modified":"2025-01-08T14:39:24","modified_gmt":"2025-01-08T06:39:24","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%80%e4%b8%aaxml%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094261.html","title":{"rendered":"Python\u5982\u4f55\u751f\u6210\u4e00\u4e2axml\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210448\/d963909d-c7e4-4ab0-9691-d5229b5e7a07.webp\" alt=\"Python\u5982\u4f55\u751f\u6210\u4e00\u4e2axml\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210XML\u6587\u4ef6\u4e3b\u8981\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528xml.etree.ElementTree\u6a21\u5757\u3001\u4f7f\u7528lxml\u5e93\u3001\u4f7f\u7528minidom\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662fxml.etree.ElementTree\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u662fPython\u5185\u7f6e\u5e93\uff0c\u7b80\u5355\u6613\u7528\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528xml.etree.ElementTree\u6a21\u5757\u751f\u6210XML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001xml.etree.ElementTree\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>xml.etree.ElementTree\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u89e3\u6790XML\u6570\u636e\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u4e14\u6709\u6548\u7684API\uff0c\u9002\u5408\u5927\u591a\u6570XML\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u521b\u5efa\u6839\u5143\u7d20\u4e0e\u5b50\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210\u4e00\u4e2aXML\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u6839\u5143\u7d20\uff0c\u7136\u540e\u5411\u6839\u5143\u7d20\u6dfb\u52a0\u5b50\u5143\u7d20\u3002\u4f7f\u7528ElementTree\u6a21\u5757\u4e2d\u7684Element\u7c7b\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u8fd9\u4e9b\u5143\u7d20\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;root&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u6dfb\u52a0\u5230\u6839\u5143\u7d20<\/strong><\/h2>\n<p>child1 = ET.SubElement(root, &quot;child1&quot;)<\/p>\n<p>child1.text = &quot;This is child1&quot;<\/p>\n<p>child2 = ET.SubElement(root, &quot;child2&quot;)<\/p>\n<p>child2.text = &quot;This is child2&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u8bbe\u7f6e\u5143\u7d20\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a\u5143\u7d20\u8bbe\u7f6e\u5c5e\u6027\uff0c\u4f7f\u7528set\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">child1.set(&quot;attribute&quot;, &quot;value&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u751f\u6210XML\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528ElementTree\u7c7b\u4e2d\u7684tostring\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210XML\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">xml_string = ET.tostring(root, encoding=&#39;unicode&#39;)<\/p>\n<p>print(xml_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u5199\u5165XML\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528ElementTree\u7c7b\u4e2d\u7684write\u65b9\u6cd5\u5c06XML\u6570\u636e\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tree = ET.ElementTree(root)<\/p>\n<p>tree.write(&quot;output.xml&quot;, encoding=&quot;utf-8&quot;, xml_declaration=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001lxml\u5e93<\/h2>\n<\/p>\n<p><h3>1\u3001\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>lxml\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5177\u6709\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6027\u80fd\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684XML\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5lxml\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u751f\u6210XML\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528lxml\u751f\u6210XML\u6587\u4ef6\u7684\u65b9\u6cd5\u7c7b\u4f3c\u4e8eElementTree\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff1a<\/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;root&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u6dfb\u52a0\u5230\u6839\u5143\u7d20<\/strong><\/h2>\n<p>child1 = etree.SubElement(root, &quot;child1&quot;)<\/p>\n<p>child1.text = &quot;This is child1&quot;<\/p>\n<p>child2 = etree.SubElement(root, &quot;child2&quot;)<\/p>\n<p>child2.text = &quot;This is child2&quot;<\/p>\n<h2><strong>\u8bbe\u7f6e\u5143\u7d20\u5c5e\u6027<\/strong><\/h2>\n<p>child1.set(&quot;attribute&quot;, &quot;value&quot;)<\/p>\n<h2><strong>\u751f\u6210XML\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>xml_string = etree.tostring(root, pretty_print=True, encoding=&#39;unicode&#39;)<\/p>\n<p>print(xml_string)<\/p>\n<h2><strong>\u5199\u5165XML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;output.xml&quot;, &quot;wb&quot;) as file:<\/p>\n<p>    file.write(etree.tostring(root, pretty_print=True, xml_declaration=True, encoding=&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001minidom\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>minidom\u6a21\u5757\u4e5f\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684DOM\u63a5\u53e3\uff0c\u9002\u5408\u5c0f\u578bXML\u6587\u4ef6\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u521b\u5efaDocument\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528minidom\u6a21\u5757\u751f\u6210XML\u6587\u4ef6\u7684\u6b65\u9aa4\u4e0e\u5176\u4ed6\u6a21\u5757\u7c7b\u4f3c\uff0c\u9996\u5148\u9700\u8981\u521b\u5efaDocument\u5bf9\u8c61\uff0c\u7136\u540e\u5411Document\u5bf9\u8c61\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from xml.dom.minidom import Document<\/p>\n<h2><strong>\u521b\u5efaDocument\u5bf9\u8c61<\/strong><\/h2>\n<p>doc = Document()<\/p>\n<h2><strong>\u521b\u5efa\u6839\u5143\u7d20\u5e76\u6dfb\u52a0\u5230Document\u5bf9\u8c61<\/strong><\/h2>\n<p>root = doc.createElement(&quot;root&quot;)<\/p>\n<p>doc.appendChild(root)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u6dfb\u52a0\u5230\u6839\u5143\u7d20<\/strong><\/h2>\n<p>child1 = doc.createElement(&quot;child1&quot;)<\/p>\n<p>child1.appendChild(doc.createTextNode(&quot;This is child1&quot;))<\/p>\n<p>root.appendChild(child1)<\/p>\n<p>child2 = doc.createElement(&quot;child2&quot;)<\/p>\n<p>child2.appendChild(doc.createTextNode(&quot;This is child2&quot;))<\/p>\n<p>root.appendChild(child2)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5143\u7d20\u5c5e\u6027<\/strong><\/h2>\n<p>child1.setAttribute(&quot;attribute&quot;, &quot;value&quot;)<\/p>\n<h2><strong>\u751f\u6210XML\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>xml_string = doc.toprettyxml(indent=&quot;  &quot;)<\/p>\n<p>print(xml_string)<\/p>\n<h2><strong>\u5199\u5165XML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;output.xml&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    file.write(xml_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210XML\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u5176\u4e2dxml.etree.ElementTree\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662fPython\u5185\u7f6e\u5e93\uff0c\u7b80\u5355\u6613\u7528\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u529f\u80fd\u66f4\u5f3a\u5927\u7684lxml\u5e93\u3002minidom\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684DOM\u63a5\u53e3\uff0c\u9002\u5408\u5c0f\u578bXML\u6587\u4ef6\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u751f\u6210XML\u6587\u4ef6\u7684\u6b65\u9aa4\u57fa\u672c\u76f8\u540c\uff1a\u521b\u5efa\u6839\u5143\u7d20\u3001\u6dfb\u52a0\u5b50\u5143\u7d20\u3001\u8bbe\u7f6e\u5143\u7d20\u5c5e\u6027\u3001\u751f\u6210XML\u5b57\u7b26\u4e32\u3001\u5199\u5165XML\u6587\u4ef6\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u751f\u6210\u7b26\u5408\u8981\u6c42\u7684XML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u751f\u6210XML\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528Python\u751f\u6210XML\u6587\u4ef6\u53ef\u4ee5\u501f\u52a9\u5185\u7f6e\u7684<code>xml.etree.ElementTree<\/code>\u5e93\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u6784\u5efa\u548c\u89e3\u6790XML\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u521b\u5efaXML\u5143\u7d20\uff0c\u8bbe\u7f6e\u5c5e\u6027\u548c\u6587\u672c\u5185\u5bb9\uff0c\u6700\u540e\u5c06\u5176\u5199\u5165\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import xml.etree.ElementTree as ET\n\nroot = ET.Element(&quot;root&quot;)\nchild = ET.SubElement(root, &quot;child&quot;)\nchild.text = &quot;\u8fd9\u662f\u5b50\u8282\u70b9\u7684\u5185\u5bb9&quot;\ntree = ET.ElementTree(root)\ntree.write(&quot;output.xml&quot;, encoding=&#39;utf-8&#39;, xml_declaration=True)\n<\/code><\/pre>\n<p>\u8be5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>output.xml<\/code>\u7684\u6587\u4ef6\uff0c\u5305\u542b\u4e00\u4e2a\u6839\u5143\u7d20\u548c\u4e00\u4e2a\u5b50\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728\u751f\u6210XML\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u683c\u5f0f\u7684\u6b63\u786e\u6027\uff1f<\/strong><br \/>\u786e\u4fdd\u751f\u6210\u7684XML\u6587\u4ef6\u683c\u5f0f\u6b63\u786e\uff0c\u9996\u5148\u8981\u6ce8\u610f\u5143\u7d20\u7684\u5d4c\u5957\u5173\u7cfb\u548c\u5c5e\u6027\u7684\u8bbe\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u63d0\u4f9b\u7684<code>ElementTree<\/code>\u7c7b\u6765\u9a8c\u8bc1\u751f\u6210\u7684XML\u7ed3\u6784\u3002\u53e6\u4e00\u4e2a\u65b9\u6cd5\u662f\u4f7f\u7528\u5728\u7ebfXML\u9a8c\u8bc1\u5de5\u5177\uff0c\u7c98\u8d34\u751f\u6210\u7684XML\u5185\u5bb9\u8fdb\u884c\u68c0\u67e5\uff0c\u4ee5\u786e\u8ba4\u662f\u5426\u7b26\u5408XML\u6807\u51c6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406XML\u6587\u4ef6\u7684\u8bfb\u53d6\u548c\u5199\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u5e93\u6765\u8bfb\u53d6\u548c\u5199\u5165XML\u6587\u4ef6\u3002\u8bfb\u53d6\u65f6\uff0c\u4f7f\u7528<code>ET.parse()<\/code>\u65b9\u6cd5\u52a0\u8f7dXML\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>getroot()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6839\u5143\u7d20\u3002\u5199\u5165\u65f6\uff0c\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8bfb\u53d6XML\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">tree = ET.parse(&#39;input.xml&#39;)\nroot = tree.getroot()\n\nfor child in root:\n    print(child.tag, child.text)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5370\u51fa\u6bcf\u4e2a\u5b50\u5143\u7d20\u7684\u6807\u7b7e\u548c\u6587\u672c\u5185\u5bb9\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9XML\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210XML\u6587\u4ef6\u4e3b\u8981\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528xml.etree.ElementTree\u6a21\u5757\u3001\u4f7f\u7528lxm [&hellip;]","protected":false},"author":3,"featured_media":1094267,"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\/1094261"}],"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=1094261"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094261\/revisions"}],"predecessor-version":[{"id":1094271,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094261\/revisions\/1094271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094267"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}