xml parsing to how to extract data
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | # -*- coding: utf-8 -*-
from xml.dom import minidom
fsock = open('parse.xml')
xmldoc = minidom.parse(fsock)
print xmldoc.toxml()
print '\n'
print "*************** Parse Child Node first ***************"
grammarNode = xmldoc.firstChild
grammarNode.childNodes
print grammarNode.childNodes[1].toxml()
print "*************** Parse Child Node Second***************"
print grammarNode.childNodes[3].toxml()
print "*************** Parse Child Node Third***************"
print grammarNode.childNodes[5].toxml()
print "\n**************** Extracting Data ******************** "
for i in range(1,6,2):
refNode = grammarNode.childNodes[i]
print "child Node"+" "+str(i)
pNode = refNode.childNodes[1]
print "Name:"+ pNode.firstChild.data
pNode = refNode.childNodes[3]
print "Age:"+ pNode.firstChild.data
pNode = refNode.childNodes[5]
print "Year:"+ pNode.firstChild.data
print "\n"
parse.xml:
"""<?xml version="1.0" ?>
<result>
<value>
<name> Abhijeet Vaidya </name>
<age> 21 </age>
<year> 1990 </year>
</value>
<value>
<name> Keerthan Pai </name>
<age> 21 </age>
<year> 1990 </year>
</value>
<value>
<name> Krishnaraj </name>
<age> 21 </age>
<year> 1990 </year>
</value>
</result>
"""
|