Copy text section replace insert

To get OPCUA Siemens working I need to take a section of an xml file copy it replacing every instance of specific text and paste it in repeating this a lot. Looking for any good ideas of how to accomplish this with script.

Example XML file would be

<UANodeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns:si="http://www.siemens.com/OPCUA/2017/SimaticNodeSetExtensions" xmlns:xsd="http://www.w3.org/2001/XMLSchema" LastModified="2024-02-23T22:46:15.474Z">
<NamespaceUris>
<Uri>http://Inputs</Uri>
</NamespaceUris>
<Models>
<Model ModelUri="http://Inputs" PublicationDate="2024-02-23T00:00:00Z" Version="1.00">
<RequiredModel ModelUri="http://opcfoundation.org/UA/" PublicationDate="2021-09-15T00:00:00Z" Version="1.04.10"/>
</Model>
</Models>
<Aliases>
<Alias Alias="Boolean">i=1</Alias>
<Alias Alias="Int16">i=4</Alias>
<Alias Alias="Int32">i=6</Alias>
<Alias Alias="Float">i=10</Alias>
<Alias Alias="HasComponent">i=47</Alias>
<Alias Alias="Organizes">i=35</Alias>
<Alias Alias="HasTypeDefinition">i=40</Alias>
</Aliases>
<Extensions>
<Extension>
<si:Generator Product="SiOME" Edition="Standard" Version="2.8.1"/>
</Extension>
<Extension>
<si:GeneratorExtension Hash="3b78fc15116496e0a5afd33a71cd2d83"/>
</Extension>
</Extensions>
<UAObject NodeId="ns=1;s="Root"."Objects"."Inputs"" BrowseName="1:Inputs" ParentNodeId="i=85">
<DisplayName>Inputs</DisplayName>
<References>
<Reference ReferenceType="Organizes" IsForward="false">i=85</Reference>
<Reference ReferenceType="HasTypeDefinition">i=14477</Reference>
</References>
</UAObject>
<UAVariable DataType="Int32" NodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"."[0]"" BrowseName="1:[0]" ParentNodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"" UserAccessLevel="3" AccessLevel="3" SymbolicName="S_0_">
<DisplayName>[0]</DisplayName>
<References>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"</Reference>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<si:VariableMapping>"TGMS_INPUT_1"."Cfg_DatabaseActions02"[0]</si:VariableMapping>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="Int32" NodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"."[1]"" BrowseName="1:[1]" ParentNodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"" UserAccessLevel="3" AccessLevel="3" SymbolicName="S_1_">
<DisplayName>[1]</DisplayName>
<References>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"</Reference>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<si:VariableMapping>"TGMS_INPUT_1"."Cfg_DatabaseActions02"[1]</si:VariableMapping>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="Int32" NodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"."[2]"" BrowseName="1:[2]" ParentNodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"" UserAccessLevel="3" AccessLevel="3" SymbolicName="S_2_">
<DisplayName>[2]</DisplayName>
<References>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"</Reference>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<si:VariableMapping>"TGMS_INPUT_1"."Cfg_DatabaseActions02"[2]</si:VariableMapping>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="Int32" NodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"."[3]"" BrowseName="1:[3]" ParentNodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"" UserAccessLevel="3" AccessLevel="3" SymbolicName="S_3_">
<DisplayName>[3]</DisplayName>
<References>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"</Reference>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<si:VariableMapping>"TGMS_INPUT_1"."Cfg_DatabaseActions02"[3]</si:VariableMapping>
</Extension>
</Extensions>
</UAVariable>

File is much longer.

But out of that I need to take

<UAVariable DataType="Int32" NodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"."[0]"" BrowseName="1:[0]" ParentNodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"" UserAccessLevel="3" AccessLevel="3" SymbolicName="S_0_">
<DisplayName>[0]</DisplayName>
<References>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"</Reference>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<si:VariableMapping>"TGMS_INPUT_1"."Cfg_DatabaseActions02"[0]</si:VariableMapping>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="Int32" NodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"."[1]"" BrowseName="1:[1]" ParentNodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"" UserAccessLevel="3" AccessLevel="3" SymbolicName="S_1_">
<DisplayName>[1]</DisplayName>
<References>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"</Reference>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<si:VariableMapping>"TGMS_INPUT_1"."Cfg_DatabaseActions02"[1]</si:VariableMapping>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="Int32" NodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"."[2]"" BrowseName="1:[2]" ParentNodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"" UserAccessLevel="3" AccessLevel="3" SymbolicName="S_2_">
<DisplayName>[2]</DisplayName>
<References>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"</Reference>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<si:VariableMapping>"TGMS_INPUT_1"."Cfg_DatabaseActions02"[2]</si:VariableMapping>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="Int32" NodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"."[3]"" BrowseName="1:[3]" ParentNodeId="ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"" UserAccessLevel="3" AccessLevel="3" SymbolicName="S_3_">
<DisplayName>[3]</DisplayName>
<References>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;s="TGMS_INPUT_1"."Cfg_DatabaseActions02"</Reference>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<si:VariableMapping>"TGMS_INPUT_1"."Cfg_DatabaseActions02"[3]</si:VariableMapping>
</Extension>
</Extensions>
</UAVariable>

This section and replace every instance of TGMS_INPUT_1 with TGMS_INPUT_2 and add that to the end of the file. and do that for 1-500 Then save it as an xml file.

ok this is what I came up with... is there a better solution?

# Program to read the entire file using read() function
headerFile = open("D:\Header.xml", "r")
header = headerFile.read()
headerFile.close()

bodyFile = open("D:\body.xml", "r")
body = bodyFile.read()
#print(body)
bodyFile.close()

outputFile = open("D:\Output.xml", "a+")
outputFile.write(header)
outputFile.write(body)

i=2
while i < 1001:
	newString = "TGMS_INPUT_" + str(i)
	newSection = body.replace("TGMS_INPUT_1", newString)
	outputFile.write(newSection)
	i+=1
outputFile.write("</UANodeSet>")
outputFile.close()

I'm not sure, but looking at your script, the one thing that pops out at me that could be improved is the loop. Using a for loop instead of a while loop would probably look better:

#...
outputFile.write(body)
for index in range(2, 1001):
	newString = "TGMS_INPUT_" + str(index)
	newSection = body.replace("TGMS_INPUT_1", newString)
	outputFile.write(newSection)
outputFile.write("</UANodeSet>")
#...