{"id":2878,"date":"2025-03-16T12:48:21","date_gmt":"2025-03-16T04:48:21","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2878"},"modified":"2025-03-16T12:48:21","modified_gmt":"2025-03-16T04:48:21","slug":"python3-xml-%e8%a7%a3%e6%9e%90-2","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-xml-%e8%a7%a3%e6%9e%90-2\/","title":{"rendered":"Python3 XML \u89e3\u6790"},"content":{"rendered":"\n<p>\u5728 Python3 \u4e2d\uff0c\u89e3\u6790\u548c\u5904\u7406 XML \u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u6a21\u5757\u6765\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u6a21\u5757\u662f <code>xml.etree.ElementTree<\/code> \u548c <code>lxml<\/code>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u6790\u5982\u4f55\u4f7f\u7528 Python3 \u8fdb\u884c XML \u89e3\u6790\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 <code>xml.etree.ElementTree<\/code> \u89e3\u6790 XML<\/strong><\/h3>\n\n\n\n<p><code>xml.etree.ElementTree<\/code> \u662f Python \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684 API \u6765\u5904\u7406 XML \u6587\u6863\u3002\u8fd9\u4e2a\u6a21\u5757\u9002\u5408\u5904\u7406\u4e0d\u592a\u590d\u6742\u7684 XML \u6587\u4ef6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.1 \u89e3\u6790 XML \u6587\u4ef6<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import xml.etree.ElementTree as ET\n\n# \u89e3\u6790 XML \u6587\u4ef6\ntree = ET.parse(\"example.xml\")  # \u52a0\u8f7d XML \u6587\u4ef6\nroot = tree.getroot()  # \u83b7\u53d6 XML \u6839\u5143\u7d20\n\n# \u6253\u5370\u6839\u5143\u7d20\u7684\u6807\u7b7e\nprint(\"Root element:\", root.tag)\n\n# \u904d\u5386\u6240\u6709\u5b50\u5143\u7d20\nfor child in root:\n    print(child.tag, child.attrib)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.2 \u89e3\u6790\u5b57\u7b26\u4e32\u4e2d\u7684 XML<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a XML \u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>fromstring()<\/code> \u65b9\u6cd5\u76f4\u63a5\u89e3\u6790\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xml.etree.ElementTree as ET\n\n# XML \u5b57\u7b26\u4e32\nxml_data = \"\"\"\n&lt;library&gt;\n    &lt;book&gt;\n        &lt;title&gt;Python Programming&lt;\/title&gt;\n        &lt;author&gt;John Doe&lt;\/author&gt;\n    &lt;\/book&gt;\n    &lt;book&gt;\n        &lt;title&gt;XML Parsing in Python&lt;\/title&gt;\n        &lt;author&gt;Jane Smith&lt;\/author&gt;\n    &lt;\/book&gt;\n&lt;\/library&gt;\n\"\"\"\n\n# \u89e3\u6790 XML \u5b57\u7b26\u4e32\nroot = ET.fromstring(xml_data)\n\n# \u6253\u5370 XML \u5143\u7d20\nfor book in root.findall(\"book\"):\n    title = book.find(\"title\").text\n    author = book.find(\"author\").text\n    print(f\"Title: {title}, Author: {author}\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.3 \u521b\u5efa XML \u6587\u6863<\/strong><\/h4>\n\n\n\n<p><code>xml.etree.ElementTree<\/code> \u4e5f\u652f\u6301\u521b\u5efa\u65b0\u7684 XML \u6587\u6863\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xml.etree.ElementTree as ET\n\n# \u521b\u5efa\u6839\u5143\u7d20\nroot = ET.Element(\"library\")\n\n# \u521b\u5efa\u5b50\u5143\u7d20\nbook1 = ET.SubElement(root, \"book\")\ntitle1 = ET.SubElement(book1, \"title\")\ntitle1.text = \"Python Programming\"\nauthor1 = ET.SubElement(book1, \"author\")\nauthor1.text = \"John Doe\"\n\n# \u521b\u5efa ElementTree \u5bf9\u8c61\ntree = ET.ElementTree(root)\n\n# \u4fdd\u5b58\u5230\u6587\u4ef6\ntree.write(\"output.xml\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.4 \u67e5\u627e\u5143\u7d20<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>find()<\/code><\/strong>: \u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><strong><code>findall()<\/code><\/strong>: \u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><strong><code>iter()<\/code><\/strong>: \u904d\u5386\u6574\u4e2a\u6811\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># \u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\ntitle = root.find(\".\/\/title\").text  # \u4f7f\u7528 XPath \u8868\u8fbe\u5f0f\u67e5\u627e\nprint(\"First title:\", title)\n\n# \u67e5\u627e\u6240\u6709\u4e66\u7c4d\u5143\u7d20\nbooks = root.findall(\"book\")\nfor book in books:\n    print(book.find(\"title\").text)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.5 \u4fee\u6539 XML \u5185\u5bb9<\/strong><\/h4>\n\n\n\n<p>\u53ef\u4ee5\u4fee\u6539\u5143\u7d20\u7684\u6587\u672c\u5185\u5bb9\u6216\u5c5e\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4fee\u6539\u5143\u7d20\u7684\u6587\u672c\u5185\u5bb9\nfirst_book = root.find(\"book\")\nfirst_book.find(\"title\").text = \"Advanced Python Programming\"\n\n# \u4fee\u6539\u5143\u7d20\u7684\u5c5e\u6027\nfirst_book.set(\"year\", \"2021\")\n\n# \u4fdd\u5b58\u4fee\u6539\u540e\u7684 XML\ntree.write(\"modified.xml\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528 <code>lxml<\/code> \u89e3\u6790 XML<\/strong><\/h3>\n\n\n\n<p><code>lxml<\/code> \u662f\u4e00\u4e2a\u529f\u80fd\u66f4\u5f3a\u5927\u7684\u5e93\uff0c\u652f\u6301 XPath \u548c XSLT \u7b49\u9ad8\u7ea7\u529f\u80fd\u3002\u4e0e <code>xml.etree.ElementTree<\/code> \u76f8\u6bd4\uff0c<code>lxml<\/code> \u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7279\u6027\u548c\u66f4\u9ad8\u6548\u7684\u6027\u80fd\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5927\u578b XML \u6587\u4ef6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.1 \u5b89\u88c5 <code>lxml<\/code><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install lxml<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2 \u89e3\u6790 XML \u6587\u4ef6<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from lxml import etree\n\n# \u89e3\u6790 XML \u6587\u4ef6\ntree = etree.parse(\"example.xml\")\nroot = tree.getroot()\n\n# \u6253\u5370\u6839\u5143\u7d20\u6807\u7b7e\nprint(\"Root element:\", root.tag)\n\n# \u904d\u5386\u6240\u6709\u5b50\u5143\u7d20\nfor child in root:\n    print(child.tag, child.attrib)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.3 \u4f7f\u7528 XPath \u67e5\u627e\u5143\u7d20<\/strong><\/h4>\n\n\n\n<p><code>lxml<\/code> \u652f\u6301 XPath\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u67e5\u627e\u590d\u6742\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from lxml import etree\n\n# \u89e3\u6790 XML\ntree = etree.parse(\"example.xml\")\nroot = tree.getroot()\n\n# \u4f7f\u7528 XPath \u67e5\u627e\u6240\u6709\u4e66\u7c4d\u6807\u9898\ntitles = root.xpath(\"\/\/book\/title\/text()\")\nfor title in titles:\n    print(\"Title:\", title)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.4 \u521b\u5efa\u548c\u4fee\u6539 XML \u6587\u6863<\/strong><\/h4>\n\n\n\n<p>\u4e0e <code>xml.etree.ElementTree<\/code> \u7c7b\u4f3c\uff0c<code>lxml<\/code> \u4e5f\u652f\u6301\u521b\u5efa\u548c\u4fee\u6539 XML \u6587\u6863\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from lxml import etree\n\n# \u521b\u5efa XML \u5143\u7d20\nroot = etree.Element(\"library\")\nbook1 = etree.SubElement(root, \"book\")\ntitle1 = etree.SubElement(book1, \"title\")\ntitle1.text = \"Python Programming\"\nauthor1 = etree.SubElement(book1, \"author\")\nauthor1.text = \"John Doe\"\n\n# \u521b\u5efa XML \u6811\u5e76\u5199\u5165\u6587\u4ef6\ntree = etree.ElementTree(root)\ntree.write(\"output_lxml.xml\", pretty_print=True)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.5 \u4fee\u6539 XML \u5185\u5bb9<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from lxml import etree\n\n# \u89e3\u6790 XML\ntree = etree.parse(\"example.xml\")\nroot = tree.getroot()\n\n# \u4fee\u6539\u4e66\u7c4d\u6807\u9898\nfirst_book = root.find(\".\/\/book\")\nfirst_book.find(\"title\").text = \"Advanced Python Programming\"\n\n# \u4fdd\u5b58\u4fee\u6539\u540e\u7684 XML\ntree.write(\"modified_lxml.xml\", pretty_print=True)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u5904\u7406 XML \u5c5e\u6027<\/strong><\/h3>\n\n\n\n<p>XML \u5143\u7d20\u53ef\u4ee5\u6709\u5c5e\u6027\uff0c<code>xml.etree.ElementTree<\/code> \u548c <code>lxml<\/code> \u90fd\u652f\u6301\u8bbf\u95ee\u548c\u4fee\u6539\u5c5e\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528 xml.etree.ElementTree\nroot = ET.Element(\"library\")\nbook = ET.SubElement(root, \"book\", year=\"2021\")\nbook.set(\"author\", \"John Doe\")\n\nprint(book.attrib)  # \u6253\u5370\u5c5e\u6027<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528 lxml\nbook = etree.Element(\"book\", year=\"2021\", author=\"John Doe\")\nprint(book.attrib)  # \u6253\u5370\u5c5e\u6027<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u5904\u7406 XML \u547d\u540d\u7a7a\u95f4<\/strong><\/h3>\n\n\n\n<p>XML \u547d\u540d\u7a7a\u95f4\uff08namespace\uff09\u7528\u4e8e\u907f\u514d\u5143\u7d20\u540d\u79f0\u7684\u51b2\u7a81\u3002\u5982\u679c XML \u4e2d\u5305\u542b\u547d\u540d\u7a7a\u95f4\uff0c\u9700\u8981\u7279\u522b\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xml.etree.ElementTree as ET\n\nxml_data = \"\"\"\n&lt;ns:library xmlns:ns=\"http:\/\/example.com\/ns\"&gt;\n    &lt;ns:book&gt;\n        &lt;ns:title&gt;Python Programming&lt;\/ns:title&gt;\n        &lt;ns:author&gt;John Doe&lt;\/ns:author&gt;\n    &lt;\/ns:book&gt;\n&lt;\/ns:library&gt;\n\"\"\"\n\nroot = ET.fromstring(xml_data)\n\n# \u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u67e5\u627e\u5143\u7d20\nnamespaces = {\"ns\": \"http:\/\/example.com\/ns\"}\ntitle = root.find(\".\/\/ns:title\", namespaces).text\nprint(\"Title:\", title)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>xml.etree.ElementTree<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684 XML \u89e3\u6790\u4efb\u52a1\uff0c\u63d0\u4f9b\u4e86\u57fa\u672c\u7684 API\uff0c\u64cd\u4f5c\u8d77\u6765\u6bd4\u8f83\u65b9\u4fbf\u3002<\/li>\n\n\n\n<li><strong><code>lxml<\/code><\/strong>\uff1a\u529f\u80fd\u66f4\u5f3a\u5927\uff0c\u652f\u6301 XPath\u3001XSLT \u7b49\u9ad8\u7ea7\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u6216\u5927\u578b\u7684 XML \u6587\u6863\u3002<\/li>\n\n\n\n<li>\u5728\u89e3\u6790 XML \u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u57fa\u4e8e\u5143\u7d20\u6807\u7b7e\u6216\u8005\u5c5e\u6027\u6765\u67e5\u627e\u6240\u9700\u5185\u5bb9\uff0c<code>lxml<\/code> \u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u5c24\u5176\u5728\u8fdb\u884c XPath \u67e5\u8be2\u65f6\u3002<\/li>\n\n\n\n<li>\u89e3\u6790 XML \u540e\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u4fee\u6539 XML \u5185\u5bb9\uff0c\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python3 \u4e2d\uff0c\u89e3\u6790\u548c\u5904\u7406 XML \u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u6a21\u5757\u6765\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u6a21\u5757\u662f xml.etree.E [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2879,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2878","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2878"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2878\/revisions"}],"predecessor-version":[{"id":2880,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2878\/revisions\/2880"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2879"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}