Modifying Python xml Content

Asked 2 years ago, Updated 2 years ago, 62 views

I would like to modify the xml file as below using xml.etree.ElementTree.

<?xml version="1.0" encoding="UTF-8"?>
<FAMILY
    CATEGORY        =  "Old Category"
    NAME                =  "Old Name"
    >

By the way,

import xml.etree.ElementTree import parse

xml = parse(file)
root = xml.getroot()
category = root.get('CATEGORY')
name = root.get('NAME')

category = "New Category"
name = "New Name"

xml.write(file)

The indentation and string changes that I used in the beginning are all gone In the first line again, this part is also flying away... Is there a better way to modify the xml file in Python?

python xml

2022-09-22 11:00

1 Answers

xml.write(file, encoding="utf-8", xml_declaration=True)


2022-09-22 11:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.