{"id":969472,"date":"2024-12-27T05:17:15","date_gmt":"2024-12-26T21:17:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/969472.html"},"modified":"2024-12-27T05:17:18","modified_gmt":"2024-12-26T21:17:18","slug":"xml%e5%a6%82%e4%bd%95%e7%94%a8python%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/969472.html","title":{"rendered":"xml\u5982\u4f55\u7528Python\u89e3\u6790"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183724\/22eb29df-b2bb-4f56-a4cd-72e16ad0be78.webp\" alt=\"xml\u5982\u4f55\u7528Python\u89e3\u6790\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u89e3\u6790XML\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>xml.etree.ElementTree<\/code>\u3001<code>minidom<\/code>\uff0c\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982<code>lxml<\/code>\u3001<code>BeautifulSoup<\/code>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\uff0c<code>xml.etree.ElementTree<\/code>\u7b80\u5355\u6613\u7528\u3001\u9002\u5408\u5927\u591a\u6570XML\u89e3\u6790\u4efb\u52a1\uff0c<code>lxml<\/code>\u529f\u80fd\u66f4\u5f3a\u5927\u3001\u652f\u6301XPath\u548cXSLT\uff0c<code>BeautifulSoup<\/code>\u66f4\u9002\u5408\u5904\u7406\u4e0d\u89c4\u5219\u7684XML\u6216HTML\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e38\u89c1\u7684XML\u89e3\u6790\u4efb\u52a1\uff0c\u63a8\u8350\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u5e93\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u89e3\u6790XML\u6570\u636e\uff0c\u5e76\u901a\u8fc7\u5177\u4f53\u793a\u4f8b\u5e2e\u52a9\u4f60\u9009\u62e9\u6700\u9002\u5408\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u89e3\u6790XML<\/strong><\/h2>\n<p><p><code>xml.etree.ElementTree<\/code>\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u4e14\u9ad8\u6548\u7684API\u6765\u5904\u7406XML\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p><code>xml.etree.ElementTree<\/code>\u5141\u8bb8\u4f60\u4ece\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\u4e2d\u89e3\u6790XML\uff0c\u5e76\u901a\u8fc7\u6811\u5f62\u7ed3\u6784\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u89e3\u6790XML<\/strong><\/h2>\n<p>xml_data = &quot;&quot;&quot;&lt;data&gt;<\/p>\n<p>                &lt;country name=&quot;Liechtenstein&quot;&gt;<\/p>\n<p>                    &lt;rank&gt;1&lt;\/rank&gt;<\/p>\n<p>                    &lt;year&gt;2008&lt;\/year&gt;<\/p>\n<p>                &lt;\/country&gt;<\/p>\n<p>             &lt;\/data&gt;&quot;&quot;&quot;<\/p>\n<p>root = ET.fromstring(xml_data)<\/p>\n<h2><strong>\u4ece\u6587\u4ef6\u89e3\u6790XML<\/strong><\/h2>\n<h2><strong>tree = ET.parse(&#39;data.xml&#39;)<\/strong><\/h2>\n<h2><strong>root = tree.getroot()<\/strong><\/h2>\n<p>for country in root.findall(&#39;country&#39;):<\/p>\n<p>    name = country.get(&#39;name&#39;)<\/p>\n<p>    rank = country.find(&#39;rank&#39;).text<\/p>\n<p>    print(f&quot;Country: {name}, Rank: {rank}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>fromstring()<\/code>\u65b9\u6cd5\u4eceXML\u5b57\u7b26\u4e32\u4e2d\u89e3\u6790\u6570\u636e\uff0c\u5e76\u904d\u5386<code>country<\/code>\u5143\u7d20\u6765\u63d0\u53d6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4fee\u6539XML<\/h2>\n<\/p>\n<p><p>ElementTree\u4e0d\u4ec5\u53ef\u4ee5\u89e3\u6790\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u548c\u4fee\u6539XML\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u65b0\u5143\u7d20<\/p>\n<p>new_country = ET.Element(&#39;country&#39;, name=&#39;Newland&#39;)<\/p>\n<p>rank = ET.SubElement(new_country, &#39;rank&#39;)<\/p>\n<p>rank.text = &#39;3&#39;<\/p>\n<p>root.append(new_country)<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684XML<\/strong><\/h2>\n<p>ET.dump(root)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>Element<\/code>\u548c<code>SubElement<\/code>\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u6dfb\u52a0\u65b0\u7684\u5143\u7d20\u548c\u5b50\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u4fdd\u5b58XML<\/h2>\n<\/p>\n<p><p>\u4fee\u6539\u540e\u7684XML\u53ef\u4ee5\u4fdd\u5b58\u56de\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tree = ET.ElementTree(root)<\/p>\n<p>tree.write(&#39;output.xml&#39;, encoding=&#39;utf-8&#39;, xml_declaration=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u628a\u4fee\u6539\u540e\u7684XML\u7ed3\u6784\u5199\u5165\u5230<code>output.xml<\/code>\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528<code>minidom<\/code>\u89e3\u6790XML<\/strong><\/h2>\n<p><p><code>minidom<\/code>\u4e5f\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u9002\u5408\u9700\u8981\u66f4\u8be6\u7ec6\u6587\u6863\u5bf9\u8c61\u6a21\u578b\uff08DOM\uff09\u652f\u6301\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u4e0e<code>ElementTree<\/code>\u7c7b\u4f3c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>minidom<\/code>\u89e3\u6790XML\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from xml.dom import minidom<\/p>\n<h2><strong>\u89e3\u6790XML\u6587\u4ef6<\/strong><\/h2>\n<p>xmldoc = minidom.parseString(xml_data)<\/p>\n<h2><strong>\u83b7\u53d6\u5143\u7d20<\/strong><\/h2>\n<p>countries = xmldoc.getElementsByTagName(&#39;country&#39;)<\/p>\n<p>for country in countries:<\/p>\n<p>    name = country.getAttribute(&#39;name&#39;)<\/p>\n<p>    rank = country.getElementsByTagName(&#39;rank&#39;)[0].firstChild.nodeValue<\/p>\n<p>    print(f&quot;Country: {name}, Rank: {rank}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>minidom<\/code>\u63d0\u4f9b\u4e86\u5bf9DOM\u7684\u5168\u9762\u652f\u6301\uff0c\u4f7f\u5f97\u5904\u7406\u590d\u6742XML\u7ed3\u6784\u65f6\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u521b\u5efa\u548c\u4fee\u6539XML<\/h2>\n<\/p>\n<p><p><code>minidom<\/code>\u652f\u6301\u521b\u5efa\u65b0\u7684XML\u6587\u6863\u5e76\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u65b0\u6587\u6863<\/p>\n<p>doc = minidom.Document()<\/p>\n<p>root = doc.createElement(&#39;data&#39;)<\/p>\n<p>doc.appendChild(root)<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>country = doc.createElement(&#39;country&#39;)<\/p>\n<p>country.setAttribute(&#39;name&#39;, &#39;Newland&#39;)<\/p>\n<p>root.appendChild(country)<\/p>\n<p>rank = doc.createElement(&#39;rank&#39;)<\/p>\n<p>rank.appendChild(doc.createTextNode(&#39;3&#39;))<\/p>\n<p>country.appendChild(rank)<\/p>\n<h2><strong>\u8f93\u51faXML<\/strong><\/h2>\n<p>print(doc.toprettyxml(indent=&quot;  &quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>Document<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u65b0\u7684XML\u7ed3\u6784\uff0c\u5e76\u901a\u8fc7<code>createElement<\/code>\u7b49\u65b9\u6cd5\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528<code>lxml<\/code>\u89e3\u6790XML<\/strong><\/h2>\n<p><p><code>lxml<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5<code>lxml<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>lxml<\/code>\u89e3\u6790XML\u4e0e<code>ElementTree<\/code>\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<h2><strong>\u89e3\u6790XML<\/strong><\/h2>\n<p>root = etree.fromstring(xml_data)<\/p>\n<p>for country in root.findall(&#39;country&#39;):<\/p>\n<p>    name = country.get(&#39;name&#39;)<\/p>\n<p>    rank = country.find(&#39;rank&#39;).text<\/p>\n<p>    print(f&quot;Country: {name}, Rank: {rank}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001XPath\u652f\u6301<\/h2>\n<\/p>\n<p><p><code>lxml<\/code>\u652f\u6301XPath\uff0c\u4f7f\u5f97\u9009\u62e9\u5143\u7d20\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528XPath\u9009\u62e9\u5143\u7d20<\/p>\n<p>for country in root.xpath(&#39;\/\/country&#39;):<\/p>\n<p>    name = country.get(&#39;name&#39;)<\/p>\n<p>    rank = country.xpath(&#39;rank\/text()&#39;)[0]<\/p>\n<p>    print(f&quot;Country: {name}, Rank: {rank}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>XPath\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u67e5\u8be2\u80fd\u529b\uff0c\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u7b5b\u9009\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>3\u3001XSLT\u652f\u6301<\/h2>\n<\/p>\n<p><p><code>lxml<\/code>\u8fd8\u652f\u6301XSLT\u8f6c\u6362\uff0c\u53ef\u4ee5\u5c06XML\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">xslt_root = etree.XML(&#39;&#39;&#39;&lt;xsl:stylesheet version=&quot;1.0&quot;<\/p>\n<p>    xmlns:xsl=&quot;http:\/\/www.w3.org\/1999\/XSL\/Transform&quot;&gt;<\/p>\n<p>    &lt;xsl:template match=&quot;\/&quot;&gt;<\/p>\n<p>        &lt;html&gt;<\/p>\n<p>        &lt;body&gt;<\/p>\n<p>        &lt;h2&gt;Countries&lt;\/h2&gt;<\/p>\n<p>        &lt;ul&gt;<\/p>\n<p>            &lt;xsl:for-each select=&quot;data\/country&quot;&gt;<\/p>\n<p>            &lt;li&gt;&lt;xsl:value-of select=&quot;@name&quot;\/&gt;: &lt;xsl:value-of select=&quot;rank&quot;\/&gt;&lt;\/li&gt;<\/p>\n<p>            &lt;\/xsl:for-each&gt;<\/p>\n<p>        &lt;\/ul&gt;<\/p>\n<p>        &lt;\/body&gt;<\/p>\n<p>        &lt;\/html&gt;<\/p>\n<p>    &lt;\/xsl:template&gt;<\/p>\n<p>&lt;\/xsl:stylesheet&gt;&#39;&#39;&#39;)<\/p>\n<p>transform = etree.XSLT(xslt_root)<\/p>\n<p>result = transform(root)<\/p>\n<p>print(str(result))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7XSLT\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06XML\u6570\u636e\u683c\u5f0f\u5316\u4e3aHTML\u7b49\u5176\u4ed6\u683c\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528<code>BeautifulSoup<\/code>\u89e3\u6790XML<\/strong><\/h2>\n<p><p><code>BeautifulSoup<\/code>\u5e38\u7528\u4e8e\u89e3\u6790HTML\uff0c\u4f46\u540c\u6837\u53ef\u4ee5\u7528\u4e8eXML\uff0c\u7279\u522b\u662f\u5f53XML\u683c\u5f0f\u4e0d\u89c4\u5219\u65f6\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u9996\u5148\u5b89\u88c5<code>BeautifulSoup<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>BeautifulSoup<\/code>\u89e3\u6790XML\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u89e3\u6790XML<\/strong><\/h2>\n<p>soup = BeautifulSoup(xml_data, &#39;xml&#39;)<\/p>\n<p>for country in soup.find_all(&#39;country&#39;):<\/p>\n<p>    name = country[&#39;name&#39;]<\/p>\n<p>    rank = country.rank.string<\/p>\n<p>    print(f&quot;Country: {name}, Rank: {rank}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>BeautifulSoup<\/code>\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684API\uff0c\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u4e0d\u89c4\u5219\u7684XML\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u4e0d\u89c4\u5219XML<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u683c\u5f0f\u4e0d\u4e00\u81f4\u7684XML\u65f6\uff0c<code>BeautifulSoup<\/code>\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbeXML\u4e2d\u6709\u683c\u5f0f\u4e0d\u89c4\u5219\u7684\u90e8\u5206<\/p>\n<p>irregular_xml = &quot;&quot;&quot;&lt;data&gt;<\/p>\n<p>                    &lt;country name=&quot;Unknown&quot;&gt;<\/p>\n<p>                        &lt;rank&gt;unknown&lt;\/rank&gt;<\/p>\n<p>                    &lt;country name=&quot;Newland&quot;&gt;<\/p>\n<p>                        &lt;rank&gt;3&lt;\/rank&gt;<\/p>\n<p>                &lt;\/data&gt;&quot;&quot;&quot;<\/p>\n<p>soup = BeautifulSoup(irregular_xml, &#39;xml&#39;)<\/p>\n<p>for country in soup.find_all(&#39;country&#39;):<\/p>\n<p>    print(f&quot;Country: {country.get(&#39;name&#39;)}, Rank: {country.rank.string}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>BeautifulSoup<\/code>\u80fd\u591f\u89e3\u6790\u5373\u4f7f\u662f\u683c\u5f0f\u4e0d\u5b8c\u6574\u7684XML\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\u89e3\u6790XML\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<code>xml.etree.ElementTree<\/code>\u9002\u5408\u5927\u591a\u6570\u7b80\u5355\u4efb\u52a1\uff0c<code>minidom<\/code>\u63d0\u4f9b\u8be6\u7ec6\u7684DOM\u652f\u6301\uff0c<code>lxml<\/code>\u529f\u80fd\u5f3a\u5927\u652f\u6301XPath\u548cXSLT\uff0c\u800c<code>BeautifulSoup<\/code>\u5219\u9002\u5408\u5904\u7406\u4e0d\u89c4\u5219\u7684XML\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u5c06\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5904\u7406XML\u6570\u636e\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7Python\u5f3a\u5927\u7684\u751f\u6001\u7cfb\u7edf\u8f7b\u677e\u5b9e\u73b0XML\u89e3\u6790\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u89e3\u6790XML\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u89e3\u6790XML\u6587\u4ef6\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u5b9e\u73b0\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u6709<code>xml.etree.ElementTree<\/code>\u3001<code>lxml<\/code>\u548c<code>xmltodict<\/code>\u3002<code>ElementTree<\/code>\u662fPython\u5185\u7f6e\u7684\u5e93\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7b80\u5355\u7684XML\u6587\u4ef6\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u590d\u6742\u7684XML\u6587\u6863\u6216\u8005\u9700\u8981\u66f4\u9ad8\u6548\u7684\u89e3\u6790\uff0c<code>lxml<\/code>\u5219\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002\u4f7f\u7528<code>xmltodict<\/code>\u53ef\u4ee5\u5c06XML\u6587\u4ef6\u8f6c\u6362\u4e3aPython\u5b57\u5178\uff0c\u4f7f\u5f97\u6570\u636e\u5904\u7406\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<p><strong>Python\u89e3\u6790XML\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u89e3\u6790XML\u65f6\uff0c\u7528\u6237\u5e38\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\uff0c\u5982XML\u683c\u5f0f\u4e0d\u6b63\u786e\u3001\u7f3a\u5c11\u5fc5\u9700\u7684\u6807\u7b7e\u6216\u5c5e\u6027\u3001\u7f16\u7801\u95ee\u9898\u7b49\u3002\u786e\u4fddXML\u6587\u4ef6\u7b26\u5408\u6807\u51c6\u683c\u5f0f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6b64\u5916\uff0c\u9519\u8bef\u5904\u7406\u4e5f\u5e94\u5f53\u5230\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6355\u83b7\u89e3\u6790\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u89e3\u6790\u540e\u7684XML\u6570\u636e\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff1f<\/strong><br \/>\u89e3\u6790\u540e\u7684XML\u6570\u636e\u53ef\u4ee5\u8f7b\u677e\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff0c\u4f8b\u5982JSON\u6216CSV\u3002\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u53ef\u4ee5\u5c06Python\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u800c\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u5219\u53ef\u4ee5\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4efb\u52a1\u4e2d\u7075\u6d3b\u4f7f\u7528\u89e3\u6790\u540e\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u89e3\u6790XML\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684xml.etree.ElementTre [&hellip;]","protected":false},"author":3,"featured_media":969475,"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\/969472"}],"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=969472"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969472\/revisions"}],"predecessor-version":[{"id":969476,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969472\/revisions\/969476"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/969475"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=969472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=969472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=969472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}