擅长:python、mysql、java
<p>您可以使用<a href="https://docs.python.org/3/library/xml.etree.elementtree.html#modifying-an-xml-file" rel="nofollow">ElementTree API</a>轻松修改<code>xml</code>文件</p>
<pre><code>from xml.etree.ElementTree import parse
doc = parse('data.xml')
root = doc.getroot()
for t in root.iterfind('pubgroup/member'):
t.attrib['objlocation'] = "spam"
doc.write('output.xml', xml_declaration=True)
</code></pre>
<p><a href="https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.iterfind" rel="nofollow">^{<cd2>}</a>方法返回一个<code>generator</code>函数,而不是<code>list</code>函数,如果您的<code>xml</code>文件非常大,这非常方便</p>
<p>输出</p>
^{pr2}$
<p/>
<p>这里<code>spam</code>是<code>objlocation</code>新值。在</p>