0

I need my XML attribute value pairs to be re-arranged in a specific order. This is because I run a third party tool to process these files and it looks for the attributes in a specific order.

I would like to have a list that details the order I specify

I've searched but been unable to find anything that does what I want.

order = ["attrib1","attrib2","attrib3",.........]

XML Before:

<signal id = "signal1"
atrib6 = "value6"
atrib5 = "value5"
atrib4 = "value4"
atrib3 = "value3"
atrib2 = "value2"
atrib1 = "value1"/>
<signal id = "signal2"
atrib1 = "value1"
atrib7 = "value7"
atrib2 = "value2"
atrib4 = "value4"
atrib8 = "value8"
atrib15 = "value15"/>
<signal id = "signal3"
atrib10 = "value10"
atrib17 = "value17"
atrib3 = "value3"
atrib1 = "value1"
atrib6 = "value6"
atrib22 = "value22"/>

XML After

<signal id = "signal1"
atrib1 = "value1"
atrib2 = "value2"
atrib3 = "value3"
atrib4 = "value4"
atrib5 = "value5"
atrib6 = "value6"/>
<signal id = "signal2"
atrib1 = "value1"
atrib2 = "value2"
atrib4 = "value4"
atrib7 = "value7"
atrib8 = "value8"
atrib15 = "value15"/>
<signal id = "signal3"
atrib1 = "value1"
atrib3 = "value3"
atrib6 = "value6"
atrib10 = "value10"
atrib17 = "value17"
atrib22 = "value22"/>

I am aware that the XML specification says that the attribute order is not important. However, the 3rd party software tool will not work unless the attributes are in a specific order.

Rob
  • 151
  • 1
  • 2
  • 11
  • This answer may help: https://stackoverflow.com/a/14258802/2570277 – Nick Mar 20 '19 at 09:09
  • That patch to ElementTree.py is more for maintaining the attribute order when using that module. I want to be able to specify a new order. – Rob Mar 20 '19 at 09:15
  • @Rob Nick's intention was obviously to give you a hint at a possible solution (which part of ElementTree can be monkeypatched to control the attributes order). This should actually be enough to solve your issue by customising this part of the code to match your own needs. – bruno desthuilliers Mar 20 '19 at 09:24
  • if I came across as rude then it wasn't my intention. I had already stumbled upon that link before and I don't believe that monkey hack would have helped without a deeper understanding of the library and a significant amount of research, which is why I was querying to see if anyone had resolved my specific problem in the past. Incidentally, I have managed to solve my issue using my own messy but effective method. – Rob Mar 20 '19 at 16:11
  • Rob - You should add your solution as an answer and accept it. Maybe it will help others in the future. – Daniel Haley Mar 21 '19 at 03:32
  • Ok will do later. – Rob Mar 21 '19 at 06:03
  • Added my solution. – Rob Mar 22 '19 at 12:00

1 Answers1

0

Ok here is my solution it turns my input file "input.xml" that looks like this:

<element-name attrib_7="value7" attrib_6="value6" attrib_5="value5" attrib_4="value4" attrib_3="value3" attrib_2="value2" attrib_1="value1" />
<element-name attrib_5="value5" attrib_4="value4" attrib_3="value3" attrib_1="value1" attrib_2="value2" attrib_6="value6" attrib_2="value2" />
<element-name attrib_6="value6" attrib_4="value4" attrib_2="value2" attrib_1="value1" />

into an output file "output.xml":

<element-name
attrib_1="value1"
attrib_2="value2"
attrib_3="value3"
attrib_4="value4"
attrib_5="value5"
attrib_6="value6"
attrib_7="value7"
/>
<element-name
attrib_1="value1"
attrib_2="value2"
attrib_3="value3"
attrib_4="value4"
attrib_5="value5"
attrib_6="value6"
/>
<element-name
attrib_1="value1"
attrib_2="value2"
attrib_4="value4"
attrib_6="value6"
/>

The code is here:

infile = open("input.xml","r")
outfile = open("output.xml","w")

xmlsorted =[]
span = 2

for line in infile:
    if "<element-name" in line and "/>" in line:
        attriblist =[]
        for i in range(0, len(line), span):
            step1 = line.split(" ")
        attriblist.append(step1)

        a,b,c,d,e,f,g,h,i = "","","","","","","","",""
        sortedattribs = []
        for attrib in step1:
            if "<element-name" in attrib:
                a = attrib + "\n"
            if "attrib_1=" in attrib:
                b = attrib + "\n"
            if "attrib_2=" in attrib:
                c = attrib + "\n"
            if "attrib_3=" in attrib:
                d = attrib + "\n"
            if "attrib_4=" in attrib:
                e = attrib + "\n"
            if "attrib_5=" in attrib:
                f = attrib + "\n"       
            if "attrib_6=" in attrib:
                g = attrib + "\n"   
            if "attrib_7=" in attrib:
                h = attrib + "\n"
            if "/>" in attrib:
                print("test123")
                i = attrib

        if a != "":
            sortedattribs.append(a)
        if b != "":
            sortedattribs.append(b)
        if c != "":
            sortedattribs.append(c)
        if d != "":
            sortedattribs.append(d)
        if e != "":
            sortedattribs.append(e)
        if f != "":
            sortedattribs.append(f)
        if g != "":
            sortedattribs.append(g)
        if h != "":
            sortedattribs.append(h)
        if i != "":
            sortedattribs.append(i)

        a,b,c,d,e,f,g,h,i = "","","","","","","","",""
        xmlsorted.append(sortedattribs)
        outfile.writelines(sortedattribs)

infile.close()
outfile.close()
Rob
  • 151
  • 1
  • 2
  • 11