I'm new in Python and I need some help. I try to rearrange an xml by using LXML sorting the "Rule"-Tags by the "Description". I can address the DescriptionTags by using:
for elem in root.iter('{http://www.ProgramConfiguration/2.1}Description'):
print(elem.text)
But I can't use it for sorting.
I want to transform this:
<?xml version="1.0" encoding="utf-8"?>
<ProgramConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ProgramConfiguration/2.1">
<Rules>
<Rule RuleId="1" Enabled="true">
<Description>Muster, Alex</Description>.
<WatchDirectories>
<WatchDirectory Path="\\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="2" Enabled="true">
<Description>Albert, Peter</Description>
<WatchDirectories>
<WatchDirectory Path="\\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="3" Enabled="true">
<Description>Rich, Sam</Description>
<WatchDirectories>
<WatchDirectory Path="\\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="4" Enabled="true">
<Description>Albert, Zack</Description>
<WatchDirectories>
<WatchDirectory Path="\\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
</Rules>
</ProgramConfiguration>
Into this:
<?xml version="1.0" encoding="utf-8"?>
<ProgramConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ProgramConfiguration/2.1">
<Rules>
<Rule RuleId="2" Enabled="true">
<Description>Albert, Peter</Description>
<WatchDirectories>
<WatchDirectory Path="\\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="4" Enabled="true">
<Description>Albert, Zack</Description>
<WatchDirectories>
<WatchDirectory Path="\\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="1" Enabled="true">
<Description>Muster, Alex</Description>
<WatchDirectories>
<WatchDirectory Path="\\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="3" Enabled="true">
<Description>Rich, Sam</Description>
<WatchDirectories>
<WatchDirectory Path="\\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
</Rules>
</ProgramConfiguration>
I much appreciate any help.
Dennis
Unfortunatally I can't explain more but I have to add some more details to my post then code. So I have to write some more words even I don't want this only to fill space in this. Wow I have to write a lot additional stuff to submite this question. Sorry for that, but my code example is as long as it is. so sorry again.