Skip to content
Advertisement

How to update xml file using python beautifulsoup

I have a xml file for which I have to update a value of tag. Below is the content of the file

<annotation>
    <folder>train</folder>
    <filename>Arms1.jpg</filename>
    <path>D:PyCharmWorkSpaceTensorflowworkspacetraining_demoimagestrainArms1.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
</annotation>

In the above content, I have to update the value of path with new valueBelow is the code I have:

from bs4 import BeautifulSoup
import os

curr_dir = os.path.join(os.path.dirname(__file__), 'img')
files = os.listdir(curr_dir)

for file in files:
    if ".xml" in file:
        file_path = os.path.join(curr_dir, file)
        with open(file_path, 'r') as f:
            xml_data = f.read()
        bs_data = BeautifulSoup(xml_data, "xml")
        bs_data.path.string = "C:"
        xml_file = open(file_path, "w")
        xml_file.write(bs_data.prettify())

But its not getting updated in xml file. Can anyone please help. Thanks

Advertisement

Answer

Using ElementTree (no need for any external lib)

import xml.etree.ElementTree as ET


xml = '''<annotation>
    <folder>train</folder>
    <filename>Arms1.jpg</filename>
    <path>D:PyCharmWorkSpaceTensorflowworkspacetraining_demoimagestrainArms1.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
</annotation>'''

root = ET.fromstring(xml)
root.find('path').text = 'new path value goes here'
ET.dump(root)

output

<annotation>
    <folder>train</folder>
    <filename>Arms1.jpg</filename>
    <path>new path value goes here</path>
    <source>
        <database>Unknown</database>
    </source>
</annotation>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement