我需要按特定顺序重新排列XML属性值对。这是因为我运行了第三方工具来处理这些文件,并且它会按特定顺序查找属性。
我想要一个详细列出我指定顺序的列表
我已经搜索了,但是找不到能满足我需求的东西。
order = ["attrib1","attrib2","attrib3",.........]
之前的XML
<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之后
<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"/>
我知道XML规范说属性顺序并不重要。但是,除非属性按特定顺序排列,否则第三方软件工具将无法工作。
答案 0 :(得分:0)
好,这是我的解决方案,它将我的输入文件变成“ input.xml”,如下所示:
<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" />
放入输出文件“ 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"
/>
代码在这里:
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()