Python Forum
Parsing xml file deletes whitespaces. How to avoid it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing xml file deletes whitespaces. How to avoid it?
#1
Hello, I made a program that takes one main XML file and update there attribute values = "Fail" based on other XML file attribute values. It works fine but I have one issue:

After parsing XML file it deletes whitespaces. Not only from the updated values but also from the main file. Is there any way to avoid it?

There is a picture showing someway what is my concerne:

[Image: KTrht.png]

I want to keep these values with more than one line.

Here is my code:


 import xml.etree.ElementTree as ET Mainfile = 'Mainfile_1.xml' tree = ET.parse(Mainfile) root = tree.getroot() fixfile = 'fixfile_1.xml' tree2 = ET.parse(fixfile) root2 = tree2.getroot() for objects in root.iter('object'): objid = objects.attrib.get('id') for attributes in objects.getchildren(): name = attributes.attrib.get('name') value = attributes.attrib.get('value') if value == 'FAIL': for objects2 in root2.iter('object'): objid2 = objects2.attrib.get('id') for attributes2 in objects2.getchildren(): name2 = attributes2.attrib.get('name') value2 = attributes2.attrib.get('value') if objid2 == objid: if name == name2: attributes.set('value', value2) tree.write('Mainfile_1updated.xml',xml_declaration=True, encoding='UTF-8')
Here is MainXML:

<?xml version='1.0' encoding='UTF-8'?> <Module bs='Mainfile_1'> <object name='namex' number='1' id='1000'> <item name='item0' value='100'/> <item name='item00' value='100'/> </object> <object name='namey' number='2' id='1001'> <item name='item1' value='100'/> <item name='item00' value='100'/> </object> <object name='name1' number='3' id='1234'> <item name='item1' value='FAIL'/> <item name='item2' value='233	233'/> <item name='item3' value='233'/> <item name='item4' value='FAIL'/> </object> <object name='name2' number='4' id='1238'> <item name='item8' value='FAIL'/> <item name='item9' value='233'/> </object> <object name='name32' number='5' id='2345'> <item name='item1' value='111'/> <item name='item2' value='FAIL'/> </object> <object name='name4' number='6' id='2347'> <item name='item1' value='FAIL'/> <item name='item2' value='FAIL'/> <item name='item3' value='233'/> <item name='item4' value='FAIL'/> </object> </Module>
And here is fix file:

<?xml version='1.0' encoding='UTF-8'?> <Module bs='Mainfile_1'> <object id='1234'> <item name='item1' value='something something111'/> <item name='item4' value='something 1something'/> </object> <object id='1238'> <item name='item8' value='something12 1something'/> </object> <object id='2345'> <item name='item2' value='something 12something'/> </object> <object id='2347'> <item name='item1' value='something14 13of something'/> <item name='item2' value='something 11something'/> <item name='item4' value='something14 something14 something12 13something'/> </object> </Module>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading an ASCII text file and parsing data... oradba4u 2 2,801 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Python script that deletes symbols in plain text nzcan 3 2,345 Sep-05-2023, 04:03 PM
Last Post: deanhystad
Video doing data treatment on a file import-parsing a variable EmBeck87 15 7,778 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Removing leading whitespaces palladium 1 2,189 Mar-24-2023, 04:15 PM
Last Post: bowlofred
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,968 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing a syslog file ebolisa 11 8,303 Oct-10-2021, 05:15 PM
Last Post: snippsat
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 3,830 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  File Name Parsing millpond 5 8,295 Aug-26-2020, 08:04 AM
Last Post: bowlofred
  Error while parsing tables from docx file aditi 1 7,078 Jul-14-2020, 09:24 PM
Last Post: aditi
  help parsing file aslezak 2 3,761 Oct-22-2019, 03:51 PM
Last Post: aslezak

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.