31
loading...
This website collects cookies to deliver better user experience
from xml.dom import minidom
minidom.parse("filename")
from xml.dom import minidom
#parse xml file
file = minidom.parse('demo.xml')
#grab all <record> tags
records = file.getElementsByTagName("record")
print("Name------>Phone")
for record in records:
#access <name> and <phone> node of every record
name = record.getElementsByTagName("name")
phone = record.getElementsByTagName("phone")
#access data of name and phone
print(name[0].firstChild.data, end="----->")
print(phone[0].firstChild.data)
Name------>Phone
Jameson----->(080) 78168241
Colton----->(026) 53458662
Dillon----->(051) 96790901
Channing----->(014) 98829753
Note: “Our Python script and the demo.xml file are located at the same location that’s why we only specify the file name demo.txtin the minidom.parse()function. If your Python script and xml file are located at different locations, then you have to specify the absolute or relative path of the file.”
minidom and
ElementTree