我尝试使用python将命名空间(修改标记)添加到XML文档 我想出了这个简单的解决方案
我的输入
<?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>
我的代码受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>")
然而,这不会起作用,因为我有Bins,Bin&gt;,Plays,Play标签 有更简单的方法吗? 我尝试在python中使用XML相关的包但我无法轻松地使用现有的package.method找到一种方法...
我正在考虑实现状态机以在读取文件行时跟踪状态。 但它似乎有点过分....
帮助