Problems parsing a xml file
I am editing a xml file that is originally like that:
<SequencerLoopCommand id="1073"
IterationCount="2"
CommandList="1241 1242"
Name="Loop Stream IDU64ToIDU63">
<IterateLoadSizeCommand id="1241"
LoadType="STEP"
LoadUnits="KILOBITS_PER_SECOND"
LoadStart="100"
LoadEnd="200"
LoadStep="100"
CustomLoadList=""
StreamBlockList="1543"
Name="Iterate Stream IDU64ToIDU63">
</IterateLoadSize开发者_如何学GoCommand>
<WaitCommand id="1242"
WaitTime="5"
Name="Stream IDU64ToIDU63">
</WaitCommand>
</SequencerLoopCommand>
With the following code...
doc = minidom.parse("directory")
#processing
fh = open(os.path.join(self.__dirname,self.__xmlfile),"w")
doc.writexml(fh)
fh.close()
doc.unlink()
after that, my new xml is something like:
<SequencerLoopCommand CommandList="1241 1242" IterationCount="2" Name="Loop Stream IDU64ToIDU63" id="1073">
<IterateLoadSizeCommand CustomLoadList="" LoadEnd="200" LoadStart="100" LoadStep="100" LoadType="STEP" LoadUnits="KILOBITS_PER_SECOND" Name="Iterate Stream IDU64ToIDU63" StreamBlockList="1543" id="1241">
</IterateLoadSizeCommand>
<WaitCommand Name="Stream IDU64ToIDU63" WaitTime="5" id="1242">
</WaitCommand>
</SequencerLoopCommand>
I need that the new file to have the same structure as the old one. Any tip?
Thank you
I guess it's not easy by default as both XMLs are identical (from parser POV).
However, You can write custom SAX serializer that will break & ident attributes. See http://docs.python.org/library/xml.sax.handler.html#module-xml.sax.handler , but I'd say it's not worth the effort.
精彩评论