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
  I Need Help Editing My Code eddywinch82 10 1,310 Aug-29-2020, 03:54 PM
Last Post: eddywinch82
  List structure lost when multiplying Protonn 2 563 Apr-23-2020, 04:16 AM
Last Post: buran
  Code Editing Help Rememberthecreed 2 762 Feb-08-2020, 09:56 AM
Last Post: Rememberthecreed
  lost dictionary jjpy 1 604 Jul-10-2019, 12:19 PM
Last Post: scidam
  Editing Dictionaries Astrikor 7 1,772 Aug-15-2018, 05:05 PM
Last Post: Astrikor
  variable gets lost Jordy 10 4,818 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