问题
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