Python Forum

Full Version: Modify values in XML file by data from text file (without parsing)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have 2 types of files - one is base XML file where are some attribute values = "FAIL" and i the other file - text file where are values I want to use instead of those "FAIL".
Picture below explain visualy what I mean:

[Image: 31R1A.png]

I need somehow to put in in correct place and I have no idea how to do it. I have been thinking about formating this text file to another XML file and then parse them both and replace value but I has to be done easier, and It would be perfect if it is possible without parsing XML file because it destroy structure of it (deletes whitespaces, sort attributes etc.). I tought about using regex to first find in text file Object id (number before "=>"), then attribute name (string between "=>" and ":") and then find value (string between Attribute name + ":" and "<END_OF_OBJECT_LINE>" or "<COMMA>") and somehow put in in XML but also I am not sure how to do it.

XML file:

    <Module bs="Mainfile_1">
    <object id="1000" name="namex" number="1">
        <item name="item0" value="100" />
        <item name="item00" value="100" />
    </object>
    <object id="1001" name="namey" number="2">
        <item name="item1" value="100" />
        <item name="item00" value="100" />
    </object>
    <object id="1234" name="name1" number="3">
        <item name="item1" value="FAIL" />
        <item name="item2" value="233" />
        <item name="item3" value="233" />
        <item name="item4" value="FAIL" />
    </object>
    <object id="1238" name="name2" number="4">
        <item name="item8" value="FAIL" />
        <item name="item9" value="233" />
    </object>
    <object id="2345" name="name32" number="5">
        <item name="item1" value="111" />
        <item name="item2" value="FAIL" />
    </object>
    <object id="2347" name="name4" number="6">
        <item name="item1" value="FAIL" />
        <item name="item2" value="FAIL" />
        <item name="item3" value="233" />
        <item name="item4" value="FAIL" />
    </object>
    </Module>
Text file:

   1234=>item1:something11:
    
    something11<COMMA>item4:something12:
    
    12something<END_OF_OBJECT_LINE>
    1238=>item8:something12:
    
    something11:<END_OF_OBJECT_LINE>
    2345=>item2:something12:
    
    something11:<END_OF_OBJECT_LINE>
    1234=>item1:something1:
    
    something11<COMMA>item2:something:
    
    11something<COMMA>item4:something:
    
    11something<END_OF_OBJECT_LINE>
I'm not really buying your arguments for not wanting to parse the XML. Why do you think the whitespace and order of attributes matter?
(Apr-13-2022, 04:26 AM)ndc85430 Wrote: [ -> ]I'm not really buying your arguments for not wanting to parse the XML. Why do you think the whitespace and order of attributes matter?

Because I have many files like that and it need to be uploaded in software which shows requirements for software testing, and not formating it is quite iportant because it must be as clear to read as possible. Albo I want to compare edited and original file if everything is correct, but when parsing destroys the whole structure it would be impossible to check every difference in file with +100k lines