使用 elementtree 获取和更新 xml 中的值
obtaining and updating a value in xml using elementtree
我有一个 xml 文件,我想使用 Python 3.4 中的 ElementTree 来更新其中的值。我坚持使用 ElementTree 以与其他代码保持一致。
文件看起来像:
<Details>
<Attrib name="Name">bill</Attrib>
<Attrib name="Email">bill.jones@mail.com</Attrib>
<Attrib name="Phone">555-000-555</Attrib>
</Details>
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()
for child in root:
print("Tag: {0} Attrib: {1}".format(child.tag, child.attrib))
这个输出是:
Tag: Attrib Attrib: {'name': 'Name'}
Tag: Attrib Attrib: {'name': 'Email'}
Tag: Attrib Attrib: {'name': 'Phone'}
如何获取 'Phone' 的值。所以我想获取 '555-000-555 并将其更新为另一个值?
使用 text
属性:
xmlstr = '''<Details>
<Attrib name="Name">bill</Attrib>
<Attrib name="Email">bill.jones@mail.com</Attrib>
<Attrib name="Phone">555-000-555</Attrib>
</Details>'''
import xml.etree.ElementTree as ET
root = ET.fromstring(xmlstr)
for child in root:
child.text += ' (changed)'
print("Tag: {0} text: {1}".format(child.tag, child.text))
这将输出:
Tag: Attrib text: bill (changed)
Tag: Attrib text: bill.jones@mail.com (changed)
Tag: Attrib text: 555-000-555 (changed)
使用"child.text",例如:
打印(child.attrib + ": " + child.text)
要更新具有属性 [@name="Phone"]
的某个节点 Attrib
的值,简短的解决方案:
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()
phone = root.find('Attrib[@name="Phone"]')
phone.text = '777-000-777'
print(ET.dump(root)) # ET.dump() - This function should be used for debugging only.
输出:
<Details>
<Attrib name="Name">bill</Attrib>
<Attrib name="Email">bill.jones@mail.com</Attrib>
<Attrib name="Phone">777-000-777</Attrib>
</Details>
我有一个 xml 文件,我想使用 Python 3.4 中的 ElementTree 来更新其中的值。我坚持使用 ElementTree 以与其他代码保持一致。
文件看起来像:
<Details>
<Attrib name="Name">bill</Attrib>
<Attrib name="Email">bill.jones@mail.com</Attrib>
<Attrib name="Phone">555-000-555</Attrib>
</Details>
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()
for child in root:
print("Tag: {0} Attrib: {1}".format(child.tag, child.attrib))
这个输出是:
Tag: Attrib Attrib: {'name': 'Name'}
Tag: Attrib Attrib: {'name': 'Email'}
Tag: Attrib Attrib: {'name': 'Phone'}
如何获取 'Phone' 的值。所以我想获取 '555-000-555 并将其更新为另一个值?
使用 text
属性:
xmlstr = '''<Details>
<Attrib name="Name">bill</Attrib>
<Attrib name="Email">bill.jones@mail.com</Attrib>
<Attrib name="Phone">555-000-555</Attrib>
</Details>'''
import xml.etree.ElementTree as ET
root = ET.fromstring(xmlstr)
for child in root:
child.text += ' (changed)'
print("Tag: {0} text: {1}".format(child.tag, child.text))
这将输出:
Tag: Attrib text: bill (changed)
Tag: Attrib text: bill.jones@mail.com (changed)
Tag: Attrib text: 555-000-555 (changed)
使用"child.text",例如:
打印(child.attrib + ": " + child.text)
要更新具有属性 [@name="Phone"]
的某个节点 Attrib
的值,简短的解决方案:
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()
phone = root.find('Attrib[@name="Phone"]')
phone.text = '777-000-777'
print(ET.dump(root)) # ET.dump() - This function should be used for debugging only.
输出:
<Details>
<Attrib name="Name">bill</Attrib>
<Attrib name="Email">bill.jones@mail.com</Attrib>
<Attrib name="Phone">777-000-777</Attrib>
</Details>