<p>首先,你的XML被破坏了。第2行中的<code>-</code>正在破坏解析器。另外,我认为它不喜欢<code>gco:</code>s。您可以使用其他一些XML配置吗?或者这是你无法控制的东西自动生成的吗?在</p>
<p>因此,要使用Python,XML需要的外观如下:</p>
<pre><code><?xml version="1.0"?>
<note>
<to>Tove</to>
<infoaboutauthor>
<nestedprofile>
<aboutme>
<CharacterString>I am a 10th grader who likes to play ball.</CharacterString>
</aboutme>
</nestedprofile>
</infoaboutauthor>
<date>
<info_date>
<date>
<Date>2003-06-13</Date>
</date>
<datetype>
<datetype attribute="Value">
</datetype>
</datetype>
</info_date>
</date>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</code></pre>
<p>下面是实现两个目标的代码:</p>
^{pr2}$
<p><strong>更新</strong></p>
<p>就处理“gco”而言,你可以这样做:</p>
<pre><code>def replace_in_config(old, new):
with open('config.xml', 'r') as f:
text = f.read()
with open('config.xml', 'w') as f:
f.write(text.replace(old, new))
</code></pre>
<p>然后,在执行上述XML操作之前,请运行:</p>
<pre><code>replace_in_config('gco:', '_stripped')
</code></pre>
<p>然后在XMl操作完成后(当然,您需要考虑到<code>gco:Date</code>标记现在是<code>stripped_Date</code>和CharacterString标记一样),运行以下命令:</p>
<pre><code>replace_in_config('_stripped', 'gco:')
</code></pre>
<p>这将保留原始格式,并允许您使用<code>etree</code>对其进行解析。在</p>