繁体   English   中英

如何使用python lxml解析和修改xml数据

[英]How to parse and modify xml data using python lxml

我需要通过解析xml文件,使用lxml(作者和描述)修改XML标记值。 下面是我正在使用的输入文件和我需要的输出文件。 以下是我使用的代码:

输入xml文件:

<Summary>  
<Author>ABC</Author>  
<Description>ABC DATA</Description>  
<Function>24</Function>  
</Summary>

必需的输出文件:

<Summary>  
<Author>DEF</Author>  
<Description>DEF DATA</Description>  
<Function>24</Function>  
</Summary> 

from lxml import etree  
root = etree.parse(r"C:\Users\input\input.xml")  
    for elem in root.xpath('.//Author'): 
    elem.text = "DEF"  
    root.write("output.xml", pretty_print=True,xml_declaration=True,encoding="UTF-8")

这应该工作

import xml.etree.ElementTree as ET

xml = '''<root>
    <Summary>  
        <Author>ABC</Author>  
        <Description>ABC DATA</Description>  
        <Function>24</Function>  
    </Summary>
    <Summary>  
        <Author>ABC</Author>  
        <Description>ABC DATA</Description>  
        <Function>24</Function>  
    </Summary>
</root>'''

tree = ET.fromstring(xml)
for author in tree.findall('.//Summary/Author'):
    author.text = 'new author value goes here'
for desc in tree.findall('.//Summary/Description'):
    desc.text = 'new desc value goes here'

ET.dump(tree)
# call the line below if you need to save to a file
# tree.write(open('new_file.xml', 'w'))

产量

<root>
    <Summary>  
        <Author>new author value goes here</Author>  
        <Description>new desc value goes here</Description>  
        <Function>24</Function>  
    </Summary>
    <Summary>  
        <Author>new author value goes here</Author>  
        <Description>new desc value goes here</Description>  
        <Function>24</Function>  
    </Summary>
</root>

如果您只想用“DEF”替换每个“ABC”的出现,但是保留文本原样,这应该这样做:

dat = [your input above]

nodes = ['Author','Description']
for node in nodes:
    for elem in root.xpath(f'.//{node}'): 
        elem.text = elem.text.replace("ABC","DEF")  

输出是您想要的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM