Python Forum
Modify values in XML file by data from text file (without parsing)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modify values in XML file by data from text file (without parsing)
#1
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>
Reply
#2
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?
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,176 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  very newbie problem on text file zapad 2 311 Apr-12-2024, 06:50 PM
Last Post: zapad
  encrypt data in json file help jacksfrustration 1 316 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Text parsing Arik 5 498 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  file open "file not found error" shanoger 8 1,295 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Search Excel File with a list of values huzzug 4 1,316 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Replace a text/word in docx file using Python Devan 4 3,807 Oct-17-2023, 06:03 PM
Last Post: Devan
  Input network device connection info from data file edroche3rd 6 1,164 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  What data types can I use for default values? Mark17 1 578 Oct-09-2023, 02:07 PM
Last Post: buran
  Need to replace a string with a file (HTML file) tester_V 1 814 Aug-30-2023, 03:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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