0

The goal is to process a xml file which is originally :

<?xml version="1.0" encoding="utf-8"?>

<model name="cluster">
    <data>
        <datum name="name1"  set="no" get="yes" priority="yes" type="integer" description="desc. for name 1"/>
        <datum name="name2"  set="no" get="yes" priority="yes" type="integer" description="desc. for name 2"/>
        <datum name="name3"  set="no" get="yes"                type="integer" description="desc. for name 3"/>
        <datum name="name4"  set="no" get="yes"                type="integer" description="desc. for name 4"/>
        <datum name="name5"  set="no" get="yes" priority="yes" type="integer" description="desc. for name 5"/>
        <datum name="name6"  set="no" get="yes" priority="yes" type="string"  description="desc. for name 6"/>
</data>

to check the presence and fill missing priority attributes. For this, I've used elementTree as below :

def validate_file(filename):
    tree = ET.parse(filename)
    root=tree.getroot()
    for x in root.findall('data'):
        datum= x.find('datum')
        name= datum.get('name')
    for name in root.iter('datum'):
        priority = name.get('priority')        
        if not priority :
            name.set('priority', 'yes')            
    tree.write('myfile.xml')

The problem is that elementTree always sort the processed elements so the input file ends with :

<model name="cluster">
        <data>
            <datum description="desc. for name 1" get="yes" name="name1"  priority="yes" set="no"   type="integer" />
            <datum description="desc. for name 2" get="yes" name="name2"  priority="yes" set="no"   type="integer" />
            <datum description="desc. for name 3" get="yes" name="name3"  priority="yes" set="no"   type="integer" />
            <datum description="desc. for name 4" get="yes" name="name4"  priority="yes" set="no"   type="integer" />
            <datum description="desc. for name 5" get="yes" name="name5"  priority="yes" set="no"   type="integer" />
            <datum description="desc. for name 6" get="yes" name="name6"  priority="yes" set="no"   type="string" />            
    </data>
</model>

which is alphabetically ordered where the original file wasn't. Since a similar question have been posted here, I can't believe that to be able to have the same original order, one needs to modify (i.e.: patch) the elementTree source code. Finally, if ElementTree can't do this : is there a workaround to keep the same order as it's easier for humans to read it?

Community
  • 1
  • 1
dlewin
  • 1,673
  • 1
  • 19
  • 37
  • What is your question? – MattH Jul 23 '15 at 13:01
  • modified the text so the question appears more clearly – dlewin Jul 23 '15 at 13:15
  • you can't define this as "duplicate" as I've already stated myself that the previous question wasn't applicable : I can't really admit to change the source code to handle this. – dlewin Jul 23 '15 at 13:27
  • the solution to that answer is a monkey patch. You have that in your code. You're not editing the "source". – MattH Jul 23 '15 at 13:34
  • Indeed, I can't have that in my source, as defined by the poster itsefl :"it's dirty and I'd suggest using another module that better handles this scenario". I prefer a cleaner way. – dlewin Jul 23 '15 at 13:42
  • You asked for a workaround. The monkey patch is a workaround. If you'd instead asked for an XML library with arbitrary attribute ordering then that would not have been a duplicate question. – MattH Jul 23 '15 at 13:47
  • I don't think this is the place for long exchanges. Just to bring precisions I'd say that this question isn't resolved and this isn't usefull for any further reader (cf dmckee comment ) as the proposed "workaround" is nothing less than a patch which is the answer I've been refered to at start. Sorry but I can't be ok with this. – dlewin Jul 23 '15 at 15:09
  • As previously mentioned: ask a new question, but don't ask for a workaround for ElementTree to preserve attribute ordering. – MattH Jul 23 '15 at 15:20

0 Answers0