{"id":1039577,"date":"2024-12-31T12:33:45","date_gmt":"2024-12-31T04:33:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1039577.html"},"modified":"2024-12-31T12:33:49","modified_gmt":"2024-12-31T04:33:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86xml%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1039577.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5904\u7406xml\u6587\u4ef6\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/9cbf4cc9-64e3-4bb7-a620-cf29ac128aee.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5904\u7406xml\u6587\u4ef6\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5904\u7406XML\u6587\u4ef6\u7684\u5185\u5bb9\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684xml.etree.ElementTree\u6a21\u5757\u3001\u5916\u90e8\u5e93lxml\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93BeautifulSoup\u3002<\/strong>\u5176\u4e2d\uff0cxml.etree.ElementTree\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u5bf9XML\u6587\u6863\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\u7684\u57fa\u7840\u529f\u80fd\uff1blxml\u662f\u4e00\u4e2a\u529f\u80fd\u66f4\u5f3a\u5927\u4e14\u9ad8\u6548\u7684\u5e93\uff1bBeautifulSoup\u5219\u4ee5\u5176\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u8457\u79f0\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5904\u7406XML\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528xml.etree.ElementTree\u6a21\u5757<\/h3>\n<\/p>\n<p><p>xml.etree.ElementTree\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u57fa\u672c\u7684XML\u5904\u7406\u529f\u80fd\u3002\u5b83\u9002\u7528\u4e8e\u5904\u7406\u5c0f\u578b\u548c\u4e2d\u578bXML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165ElementTree\u6a21\u5757\u5e76\u8bfb\u53d6XML\u6587\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>example.xml<\/code>\u7684XML\u6587\u4ef6\uff0c\u5176\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-xml\">&lt;root&gt;<\/p>\n<p>    &lt;child&gt;<\/p>\n<p>        &lt;subchild&gt;Value&lt;\/subchild&gt;<\/p>\n<p>    &lt;\/child&gt;<\/p>\n<p>&lt;\/root&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u8be5\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<p>tree = ET.parse(&#39;example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u904d\u5386\u548c\u8bbf\u95eeXML\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6XML\u6587\u4ef6\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u548c\u8bbf\u95ee\u5176\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u6839\u5143\u7d20\u53ca\u5176\u5b50\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(root.tag)  # \u8f93\u51fa\uff1aroot<\/p>\n<p>for child in root:<\/p>\n<p>    print(child.tag, child.attrib)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4fee\u6539XML\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4fee\u6539XML\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u6dfb\u52a0\u65b0\u7684\u5b50\u5143\u7d20\u6216\u66f4\u6539\u73b0\u6709\u5143\u7d20\u7684\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_element = ET.SubElement(root, &#39;newchild&#39;)<\/p>\n<p>new_element.text = &#39;New Value&#39;<\/p>\n<p>for child in root:<\/p>\n<p>    if child.tag == &#39;child&#39;:<\/p>\n<p>        child.find(&#39;subchild&#39;).text = &#39;Updated Value&#39;<\/p>\n<p>tree.write(&#39;updated_example.xml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528lxml\u5e93<\/h3>\n<\/p>\n<p><p>lxml\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u9ad8\u6548\u7684XML\u5904\u7406\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u548c\u590d\u6742\u7684XML\u6587\u4ef6\u3002\u5b83\u63d0\u4f9b\u4e86\u6bd4xml.etree.ElementTree\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5lxml<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\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><h4>2\u3001\u8bfb\u53d6\u548c\u89e3\u6790XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4e0exml.etree.ElementTree\u7c7b\u4f3c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528lxml\u8bfb\u53d6\u548c\u89e3\u6790XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<p>tree = etree.parse(&#39;example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u904d\u5386\u548c\u8bbf\u95eeXML\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528XPath\u8868\u8fbe\u5f0f\u904d\u5386\u548c\u8bbf\u95eeXML\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for element in root.xpath(&#39;\/\/child&#39;):<\/p>\n<p>    print(element.tag, element.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4fee\u6539XML\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528lxml\u4fee\u6539XML\u5185\u5bb9\uff0c\u5e76\u4fdd\u5b58\u66f4\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_element = etree.SubElement(root, &#39;newchild&#39;)<\/p>\n<p>new_element.text = &#39;New Value&#39;<\/p>\n<p>for element in root.xpath(&#39;\/\/subchild&#39;):<\/p>\n<p>    element.text = &#39;Updated Value&#39;<\/p>\n<p>tree.write(&#39;updated_example.xml&#39;, pretty_print=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528BeautifulSoup\u5e93<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTML\u548cXML\u89e3\u6790\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u7b80\u5355\u7684XML\u6587\u4ef6\u3002\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684API\uff0c\u4fbf\u4e8e\u6211\u4eec\u5feb\u901f\u89e3\u6790\u548c\u4fee\u6539XML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5BeautifulSoup<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5BeautifulSoup\u53ca\u5176\u4f9d\u8d56\u5e93lxml\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u548c\u89e3\u6790XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u8bfb\u53d6\u548c\u89e3\u6790XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>with open(&#39;example.xml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>soup = BeautifulSoup(content, &#39;xml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u904d\u5386\u548c\u8bbf\u95eeXML\u5143\u7d20<\/h4>\n<\/p>\n<p><p>BeautifulSoup\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\uff0c\u4fbf\u4e8e\u6211\u4eec\u904d\u5386\u548c\u8bbf\u95eeXML\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for child in soup.find_all(&#39;child&#39;):<\/p>\n<p>    print(child.name, child.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4fee\u6539XML\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u4fee\u6539XML\u5185\u5bb9\uff0c\u5e76\u4fdd\u5b58\u66f4\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_element = soup.new_tag(&#39;newchild&#39;)<\/p>\n<p>new_element.string = &#39;New Value&#39;<\/p>\n<p>soup.root.append(new_element)<\/p>\n<p>for subchild in soup.find_all(&#39;subchild&#39;):<\/p>\n<p>    subchild.string = &#39;Updated Value&#39;<\/p>\n<p>with open(&#39;updated_example.xml&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(str(soup))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406XML\u6587\u4ef6\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528xml.etree.ElementTree\u3001lxml\u548cBeautifulSoup\u3002xml.etree.ElementTree\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5c0f\u578b\u548c\u4e2d\u578bXML\u6587\u4ef6\uff1blxml\u529f\u80fd\u5f3a\u5927\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u548c\u590d\u6742\u7684XML\u6587\u4ef6\uff1bBeautifulSoup\u5219\u4ee5\u5176\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u8457\u79f0\uff0c\u9002\u7528\u4e8e\u5904\u7406\u7b80\u5355\u7684XML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548cXML\u6587\u4ef6\u7684\u590d\u6742\u5ea6\u3002<\/strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u4ee5\u4fbf\u66f4\u9ad8\u6548\u5730\u5904\u7406XML\u6587\u4ef6\u5185\u5bb9\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5904\u7406XML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u89e3\u6790XML\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>xml.etree.ElementTree<\/code>\u6a21\u5757\u6765\u89e3\u6790XML\u6587\u4ef6\u3002\u9996\u5148\uff0c\u901a\u8fc7<code>ElementTree.parse()<\/code>\u65b9\u6cd5\u8bfb\u53d6XML\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>getroot()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6839\u5143\u7d20\uff0c\u63a5\u7740\u53ef\u4ee5\u904d\u5386\u8282\u70b9\u5e76\u63d0\u53d6\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import xml.etree.ElementTree as ET\n\ntree = ET.parse(&#39;file.xml&#39;)\nroot = tree.getroot()\n\nfor child in root:\n    print(child.tag, child.attrib)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4fee\u6539XML\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u4fee\u6539XML\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ElementTree<\/code>\u6a21\u5757\u52a0\u8f7d\u6587\u4ef6\uff0c\u627e\u5230\u9700\u8981\u4fee\u6539\u7684\u8282\u70b9\uff0c\u7136\u540e\u66f4\u65b0\u5176\u6587\u672c\u6216\u5c5e\u6027\u3002\u5b8c\u6210\u4fee\u6539\u540e\uff0c\u4f7f\u7528<code>ElementTree.write()<\/code>\u65b9\u6cd5\u5c06\u66f4\u6539\u4fdd\u5b58\u56de\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">tree = ET.parse(&#39;file.xml&#39;)\nroot = tree.getroot()\n\nfor elem in root.iter(&#39;tag_name&#39;):\n    elem.text = &#39;new text&#39;\n\ntree.write(&#39;file.xml&#39;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u65b0\u7684XML\u6587\u4ef6\uff1f<\/strong><br \/>\u521b\u5efa\u65b0\u7684XML\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7<code>ElementTree<\/code>\u6a21\u5757\u7684<code>Element<\/code>\u7c7b\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u6839\u5143\u7d20\u53ca\u5176\u5b50\u5143\u7d20\uff0c\u5e76\u4f7f\u7528<code>ElementTree<\/code>\u7684<code>ElementTree<\/code>\u6784\u9020\u51fd\u6570\u5c06\u5176\u5199\u5165\u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import xml.etree.ElementTree as ET\n\nroot = ET.Element(&#39;root&#39;)\nchild = ET.SubElement(root, &#39;child&#39;)\nchild.text = &#39;This is a child element&#39;\n\ntree = ET.ElementTree(root)\ntree.write(&#39;new_file.xml&#39;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5904\u7406XML\u6587\u4ef6\u7684\u5185\u5bb9\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684xml.etree.ElementTree\u6a21\u5757\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1039584,"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\/1039577"}],"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=1039577"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039577\/revisions"}],"predecessor-version":[{"id":1039585,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039577\/revisions\/1039585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039584"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1039577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1039577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1039577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}