-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlparsing.py
31 lines (24 loc) · 966 Bytes
/
xmlparsing.py
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
28
29
30
31
import xml.dom.minidom
import os
os.chdir("/workspace")
def main():
# use the parse() function to load and parse an XML file
doc = xml.dom.minidom.parse("samplexml.xml")
# print out the document node and the name of the first child tag
print (doc.nodeName)
print (doc.firstChild.tagName)
# get a list of XML tags from the document and print each one
skills = doc.getElementsByTagName("skill")
print ("%d skills:" % skills.length)
for skill in skills:
print (skill.getAttribute("name"))
# create a new XML tag and add it into the document
newSkill = doc.createElement("skill")
newSkill.setAttribute("name", "jQuery")
doc.firstChild.appendChild(newSkill)
skills = doc.getElementsByTagName("skill")
print ("%d skills:" % skills.length)
for skill in skills:
print (skill.getAttribute("name"))
if __name__ == "__main__":
main()