Demo
Import Xml.dom.minidom
Dom=xml.dom.minidom.parse (' Sample.xml ')
root = Dom.documentelement
Cc=dom.getelementsbytagname (' movie ')
C1=CC[0]
Print (Root.nodename)
Print (Root.nodevalue)
Print (Root.nodetype)
Print (root. Element_node)
Aa=root.getelementsbytagname (' movie ')
A=AA[0]
Print (' *************************** ')
Print (A.nodename)
Print (A.nodevalue)
Print (A.getattribute (' title '))
Print (A.firstchild)
Print (len (AA))
Print (A.getattribute (' title '))
Print (A.getelementsbytagname (' type ') [0].childnodes[0].data)
Print (a.getelementsbytagname (' format ') [0].childnodes[0].data)
' Parse XML using DOM '
# Coding:utf-8
Import Xml.dom.minidom
From Xml.dom.minidom Import Parse
DT = Xml.dom.minidom.parse (' sample.xml ')
COLLECTION = Dt.documentelement
If Collection.hasattribute (' shelf '):
Print (' Root element:%s '% collection.getattribute (' shelf '))
# Get all films and print detail information
MOVIES = Collection.getelementsbytagname (' movie ')
# Print detailed information for each movie
For movie in MOVIES:
Type_ = Movie.getelementsbytagname (' type ') [0]
Format_ = movie.getelementsbytagname (' format ') [0]
Rating = Movie.getelementsbytagname (' rating ') [0]
Description = Movie.getelementsbytagname (' description ') [0]
Print (' *****movie***** ')
Print (' \ttitle:%s '% movie.getattribute (' title '))
Print (' \ttype:%s '% type_.childnodes[0].data)
Print (' \tformat:%s '% format_.childnodes[0].data)
Print (' \trating:%s '% rating.childnodes[0].data)
Print (' \tdescription:%s '% description.childnodes[0].data)
Python parse XML using DOM