使用Python etree更新XML元素和属性值 [英] Updating XML elements and attribute values using Python etree
问题描述
我正在尝试使用Python 2.7的ElementTree
库来解析XML文件,然后用测试数据替换特定的元素属性,然后将其另存为唯一的XML文件.
I'm trying to use Python 2.7's ElementTree
library to parse an XML file, then replace specific element attributes with test data, then save this as a unique XML file.
我对解决方案的想法是(1)通过将文件读取为字符串从CSV文件中获取新数据;(2)在某些定界符处对字符串进行切片;(3)追加到列表中,然后( 4)使用ElementTree
用列表中的特定值更新/删除/替换属性.
My idea for a solution was to (1) source new data from a CSV file by reading a file to a string, (2) slice the string at certain delimiter marks, (3) append to a list, and then (4) use ElementTree
to update/delete/replace the attribute with a specific value from the list.
我查看了ElementTree
文档&看到了clear()
和remove()
函数,但是我不知道如何充分使用它们的语法.
I've looked in the ElementTree
documentation & saw the clear()
and remove()
functions, but I have no idea of the syntax to use them adequately.
下面是要修改的XML的示例-要替换/更新带有XXXXX
的属性:
An example of the XML to modify is below - attributes with XXXXX
are to be replaced/updated:
<TrdCaptRpt RptID="10000001" TransTyp="0">
<RptSide Side="1" Txt1="XXXXX">
<Pty ID="XXXXX" R="1"/>
</RptSide>
</TrdCaptRpt>
预期结果将是,例如:
<TrdCaptRpt RptID="10000001" TransTyp="0">
<RptSide Side="1" Txt1="12345">
<Pty ID="ABCDE" R="1"/>
</RptSide>
</TrdCaptRpt>
如何使用etree
命令更改基本XML以使用列表中的项目进行更新[]?
How do I use the etree
commands to change the base XML to update with an item from the list[]?
推荐答案
For this kind of work, I always recommend BeautifulSoup
because it has a really easy to learn API:
from BeautifulSoup import BeautifulStoneSoup as Soup
xml = """
<TrdCaptRpt RptID="10000001" TransTyp="0">
<RptSide Side="1" Txt1="XXXXX">
<Pty ID="XXXXX" R="1"/>
</RptSide>
</TrdCaptRpt>
"""
soup = Soup(xml)
rpt_side = soup.trdcaptrpt.rptside
rpt_side['txt1'] = 'Updated'
rpt_side.pty['id'] = 'Updated'
print soup
示例输出:
<trdcaptrpt rptid="10000001" transtyp="0">
<rptside side="1" txt1="Updated">
<pty id="Updated" r="1">
</pty></rptside>
</trdcaptrpt>
使用xml.etree.ElementTree
,您可以使用以下脚本:
With xml.etree.ElementTree
you could use the following script:
from xml.etree import ElementTree as etree
xml = """
<TrdCaptRpt RptID="10000001" TransTyp="0">
<RptSide Side="1" Txt1="XXXXX">
<Pty ID="XXXXX" R="1"/>
</RptSide>
</TrdCaptRpt>
"""
root = etree.fromstring(xml)
rpt_side = root.find('RptSide')
rpt_side.set('Txt1', 'Updated')
pty = rpt_side.find('Pty')
pty.set('ID', 'Updated')
print etree.tostring(root)
示例输出:
<TrdCaptRpt RptID="10000001" TransTyp="0">
<RptSide Side="1" Txt1="Updated">
<Pty ID="Updated" R="1" />
</RptSide>
</TrdCaptRpt>
这篇关于使用Python etree更新XML元素和属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!