I try to add namespace ( modify a tag ) to an XML doc using python I come up with this simple solution
My input
<?xml version="1.0" encoding="UTF-8"?>
<assData version="1.0" xsi:schemaLocation="http://xmlns.kama.com/it/eng/sgatInternal/assDataInternal/v1 /kama/it/eng/sgatInternal/assDataInternal/v1/assDataInternal-1_0.xsd" xmlns="http://xmlns.kama.com/it/eng/sgatInternal/assDataInternal/v1" xmlns:ext="http://xmlns.kama.com/it/eng/sgat/assData/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<su>suName</su>
<PlayLocation>PlayLocation</PlayLocation>
<fat>fatValue</fat>
<Product kamaProductID="4xxx9-DOG325-Bare" suProductID="4x9 C325 BS" family="9x9" project="sgat" technology="ST1"/>
<dog type="ENG" assdogID="assdogID" totalSNRCount="100"/>
<Play step="PlayStep" type="Screen"/>
<ProcessFlow>processFlow</ProcessFlow>
<Recipe name="recipeName" type="PRD" version="1.00" id="recipeID"/>
<PlayerSpecVersion>4.0</PlayerSpecVersion>
<PlayDateTime start="2013-10-23T03:36:15+08:00" end="2013-10-23T03:36:15+08:00" timeZone="Europe/Rome"/>
<ArrayType>1</ArrayType>
<FirmWAR version="3.4"/>
</Header>
<assPlayReport>
<RecSpec>
<Bins>
<Bin name="A" type="HB" number="1" pass="true"/>
<Bin name="A-" type="HB" number="2" pass="true"/>
<Bin name="B" type="HB" number="3" pass="false"/>
<Bin name="C" type="HB" number="4" pass="false"/>
<Bin name="F" type="HB" number="5" pass="false"/>
</Bins>
<Plays>
<Play upperLimit="3.18" name="Play1" unit="mm" lowerLimit="3.27"/>
<Play name="Play2"/>
<Play upperLimit="22.42" name="Play2=3" unit="mm" lowerLimit="23.10"/>
</Plays>
</RecSpec>
<Data>
<SNRs>
<SNR id="firstSNR" suSerialID="suSerialID" rework="0" startPlayDateTime="2013-10-23T03:36:15+08:00" endPlayDateTime="2013-10-23T03:36:15+08:00" fatdogID="fatdogID" fatPlateID="G" fatSubplateID="55" compositeSerialID="fatValue_fatdogIDG55_firstSNR" compositeSubplateID="fatValueG55" sourcedogID="fatdogID_S">
<Player DumpStop="DumpStop" targetActuatorID="targetActuatorID" operator="operator"/>
<Temperature unit="C">34.5</Temperature>
<Bread siteID="BreadSiteID" deviceID="BreadDeviceID" serialID="BreadSerialID" chipID="43" softWARVersion="2.1" arrayType="12" initializationFileVersion="1.0" hardWARID="BreadHardWARID" id="BreadID" firmWARVersion="3" isParallelExecution="false"/>
<Bins>
<FinalBin type="HB">4</FinalBin>
<Bin PlayName="FUNC" type="SB1">1</Bin>
<Bin PlayName="IMG" type="SB1">1</Bin>
<Bin PlayName="TUN" type="SB1">4</Bin>
<Bin PlayName="FUNC_FP" type="SB2">1</Bin>
<Bin PlayName="FUNC_PWR" type="SB2">1</Bin>
<Bin PlayName="IMG_LO" type="SB2">1</Bin>
<Bin PlayName="IMG_PROC" type="SB2">1</Bin>
<Bin PlayName="TUN_SENS" type="SB2">4</Bin>
</Bins>
<Plays>
<Play name="Play1">
<FReport>true</FReport>
<PReport>
<Result>3.19</Result>
</PReport>
</Play>
<Play name="Play2">
<FReport>false</FReport>
<PReport>
<Result>string result</Result>
</PReport>
</Play>
</Plays>
</SNR>
<SNR id="secondSNR" suSerialID="suSerialID" rework="0" startPlayDateTime="2013-10-23T03:37:15+08:00" endPlayDateTime="2013-10-23T03:38:15+08:00" fatdogID="fatdogID" fatPlateID="Q" fatSubplateID="12" compositeSerialID="fatValue_fatdogIDQ12_secondSNR" compositeSubplateID="fatValueQ12" sourcedogID="fatdogID_S">
<Player DumpStop="DumpStop" targetActuatorID="targetActuatorID" operator="operator"/>
<Temperature unit="F">34.5</Temperature>
<Bread siteID="BreadSiteID" deviceID="BreadDeviceID" serialID="BreadSerialID" chipID="43" softWARVersion="2.1" arrayType="12" initializationFileVersion="1.0" hardWARID="BreadHardWARID" id="BreadID" firmWARVersion="3" isParallelExecution="false"/>
<Bins>
<FinalBin type="HB">3</FinalBin>
<Bin PlayName="FUNC" type="SB1">1</Bin>
<Bin PlayName="IMG" type="SB1">3</Bin>
<Bin PlayName="FUNC_FP" type="SB2">1</Bin>
<Bin PlayName="FUNC_PWR" type="SB2">1</Bin>
<Bin PlayName="IMG_LO" type="SB2">3</Bin>
<Bin PlayName="IMG_PROC" type="SB2">3</Bin>
</Bins>
<Plays>
<Play name="Play1">
<FReport>true</FReport>
<PReport>
<Result>3.19</Result>
</PReport>
</Play>
<Play name="Play2">
<FReport>false</FReport>
<PReport>
<Result>string result</Result>
</PReport>
</Play>
</Plays>
</SNR>
</SNRs>
</Data>
</assPlayReport>
</assData>
My code inspired by Search and replace a line in a file in Python
from tempfile import mkstemp
from shutil import move
from os import remove, close
def replace(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with open(abs_path,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
close(fh)
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
replace("c:\\temp\\stack_add_ext_input.xml","<Bins","<ext:Bins")
replace("c:\\temp\\stack_add_ext_input.xml","/Bins>","/ext:Bins>")
#wont work with this method
#replace("c:\\temp\\stack_add_ext_input.xml","<Bin","<ext:Bin")
#replace("c:\\temp\\stack_add_ext_input.xml","/Bin>","/ext:Bin>")
#wont work with this method
replace("c:\\temp\\stack_add_ext_input.xml","<Plays","<ext:Plays")
replace("c:\\temp\\stack_add_ext_input.xml","/Plays>","/ext:Plays>")
However, this wont work as I have the Bins,Bin>,Plays,Play tag Is there a easier way to do this? I try to use the XML related package in python but I cant find a way to do it using the existing package.method easily...
I am thinking of implementing a state machine to keep track the state while reading the lines of the file. But it seems to overkill....
Thx for the help