-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathminidom.py
More file actions
27 lines (23 loc) · 847 Bytes
/
Copy pathminidom.py
File metadata and controls
27 lines (23 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import xml.dom.minidom as md
import io
if __name__ == "__main__":
dom = md.parse('../cd-list.xml')
for child in dom.childNodes[0].childNodes:
print('childnode', child)
for title in dom.getElementsByTagName('title'):
print('title', title.firstChild.nodeValue)
for track in dom.getElementsByTagName('track'):
print('track-no', track.getAttribute('no'))
root = dom.childNodes[0]
newcd = dom.createElement('cd')
artist = dom.createElement('artist')
artist.appendChild(dom.createTextNode('Bob'))
artist.setAttribute('country', 'DE')
newcd.appendChild(artist)
title = dom.createElement('title')
title.appendChild(dom.createTextNode('Song for Alice'))
newcd.appendChild(title)
root.appendChild(newcd)
fd = io.StringIO()
dom.writexml(fd)
print(fd.getvalue())