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
#2
I shall be extremely grateful if someone can help as I'm really stressed up with this
Reply
#3
What formatting is being lost? It might help of you showed a sample XML document.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Lost Control over VLC jrockow 8 1,017 Jul-18-2023, 06:04 PM
Last Post: jrockow
  Lost Modules standenman 2 685 Jun-22-2023, 12:18 PM
Last Post: standenman
  Lost Module standenman 10 2,787 Oct-30-2021, 05:11 PM
Last Post: deanhystad
  List structure lost when multiplying Protonn 2 2,201 Apr-23-2020, 04:16 AM
Last Post: buran
  lost dictionary jjpy 1 1,860 Jul-10-2019, 12:19 PM
Last Post: scidam
  variable gets lost Jordy 10 8,987 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