Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XML Editing formatting lost
#1
I'm trying to edit an xml but in the process I'm losing the formatting and thus the edited xml is not readable in the software I want to because of the messed formatting. I do not want to lose any formatting and just replace some simple text but in the process my formatting is getting lost. This is the kind of simple function I have. Please can anyone help so that my original xml formatting is retained?

def xml_parsing(file_name, tag_name, source_string_list, target_string_list):
    
    from xml.etree import ElementTree
    with open(file_name, 'rb+') as f:
        tree = ElementTree.parse(f)
        root = tree.getroot()
        element = root.find(tag_name)
        count = len(source_string_list)

        for x in range(count):
             
            element.text = element.text.replace(str(source_string_list[x]), (target_string_list[x]))
  
    tree.write(file_name, xml_declaration=True, encoding='utf-8') 
Reply


Messages In This Thread
XML Editing formatting lost - by ateestructural - Apr-07-2021, 06:58 PM
RE: XML Editing formatting lost - by ateestructural - Apr-07-2021, 07:33 PM
RE: XML Editing formatting lost - by ndc85430 - Apr-08-2021, 04:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Lost Control over VLC jrockow 8 1,129 Jul-18-2023, 06:04 PM
Last Post: jrockow
  Lost Modules standenman 2 756 Jun-22-2023, 12:18 PM
Last Post: standenman
  Need help editing a PDF omar 4 1,097 Oct-22-2022, 08:52 PM
Last Post: Larz60+
  Lost Module standenman 10 2,909 Oct-30-2021, 05:11 PM
Last Post: deanhystad
  List structure lost when multiplying Protonn 2 2,274 Apr-23-2020, 04:16 AM
Last Post: buran
  lost dictionary jjpy 1 1,912 Jul-10-2019, 12:19 PM
Last Post: scidam
  variable gets lost Jordy 10 9,216 Oct-10-2016, 08:08 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020