简体   繁体   中英

How to parse and modify xml data using python lxml

I need to modify the XML tag values using lxml (Author and Description) by parsing through xml files. below is the input file I am using and the output file I need. Below is the code I am using:

input xml file:

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

Required output file:

<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")

This should work

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'))

output

<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>

If you are only looking to replace each occurance of "ABC" with "DEF" but otherwise leave the text as-is, this should do it:

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")  

The output is your desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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