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
  Python script that deletes symbols in plain text nzcan 3 705 Sep-05-2023, 04:03 PM
Last Post: deanhystad
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,914 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Removing leading whitespaces palladium 1 731 Mar-24-2023, 04:15 PM
Last Post: bowlofred
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,703 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing a syslog file ebolisa 11 4,160 Oct-10-2021, 05:15 PM
Last Post: snippsat
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,286 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  File Name Parsing millpond 5 3,635 Aug-26-2020, 08:04 AM
Last Post: bowlofred
  Error while parsing tables from docx file aditi 1 3,743 Jul-14-2020, 09:24 PM
Last Post: aditi
  help parsing file aslezak 2 2,250 Oct-22-2019, 03:51 PM
Last Post: aslezak
  Python Script for parsing dictionary values from yaml file pawan6782 3 4,965 Sep-04-2019, 07:21 PM
Last Post: pawan6782

Forum Jump:

User Panel Messages

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