Here is sample code for how we can use dom in python for extracting data from xml.
import xml.dom.minidom
document = """<root><firstname>Anil</firstname>
<middlename>R.</middlename>
<surname>Kesariya</surname></root>"""
dom = xml.dom.minidom.parseString(document)
def getText(nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
def handleTok(tokenlist):
texts = ""
for token in tokenlist:
texts += " "+ getText(token.childNodes)
return texts
first_name = dom.getElementsByTagName("firstname")
first_name = handleTok(first_name)
middle_name = dom.getElementsByTagName("middlename")
middle_name = handleTok(middle_name)
last_name = dom.getElementsByTagName("surname")
last_name = handleTok(last_name)
print "Name Detail :::",first_name + " " + middle_name + " " + last_name
import xml.dom.minidom
document = """<root><firstname>Anil</firstname>
<middlename>R.</middlename>
<surname>Kesariya</surname></root>"""
dom = xml.dom.minidom.parseString(document)
def getText(nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
def handleTok(tokenlist):
texts = ""
for token in tokenlist:
texts += " "+ getText(token.childNodes)
return texts
first_name = dom.getElementsByTagName("firstname")
first_name = handleTok(first_name)
middle_name = dom.getElementsByTagName("middlename")
middle_name = handleTok(middle_name)
last_name = dom.getElementsByTagName("surname")
last_name = handleTok(last_name)
print "Name Detail :::",first_name + " " + middle_name + " " + last_name