How could I infuse elements with other elements in an xml using Minidom?

佐手、 提交于 2019-12-25 06:27:00

问题


I'm using minidom with Python 3.5

The original xml data looks like this: (# of Beat and EditData_Score node/tags are fixed (in this case 3 each).

<EditData_Score>
<EditDataType>BEAT_SCORE</EditDataType>
<Spot>20</Spot>
<Time>7</Time>
<Type>X</Type>
<Beat>
<EditDataType>BEAT_MARK</EditDataType>
<Spot>26</Spot>
<BeatLength>4.84472036</BeatLength>
</Beat>
</EditData_Score>
<EditData_Score>
<EditDataType>BEAT_SCORE</EditDataType>
<Spot>36</Spot>
<Time>10</Time>
<Type>X</Type>
<Beat>
<EditDataType>BEAT_MARK</EditDataType>
<Spot>48</Spot>
<BeatLength>4.84472036</BeatLength>
</Beat>
</EditData_Score>

As you can see, Beat elements are nested in EditData_Score elements. And the Spot node's value is always increasing. I want to consolidate EditData_Score elements, add some minor alterations then put and redistribute the Beats within them such that the EditData_Score encapsulates all specified beats:

<EditData_Score>
<EditDataType>BEAT_SCORE</EditDataType>
<Spot>20</Spot>
<Time>17</Time>
<Type>X</Type>
<Beat>
<EditDataType>BEAT_MARK</EditDataType>
<Spot>26</Spot>
<BeatLength>4.84472036</BeatLength>
</Beat>
<Beat>
<EditDataType>BEAT_MARK</EditDataType>
<Spot>48</Spot>
<BeatLength>4.84472036</BeatLength>
</Beat>
</EditData_Score>

I figured I could parse the xml once and:

  • add all the Beats I want to group to a list
  • add all the EditDataScores I want to keep to a list

Parse it again and:

  • Delete/Remove/Unset? all the old Beats and EditDataScores
  • Barf out the Beat Groups in the appropriate EditDataScore

来源:https://stackoverflow.com/questions/37432161/how-could-i-infuse-elements-with-other-elements-in-an-xml-using-minidom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!