[Expat-discuss] Parse XML file
amitesh kumar
amitesh116 at gmail.com
Thu Aug 9 08:11:08 CEST 2007
Hi All,
Please help me in the below code:
import sys
import xml.parsers.expat
import dircache
rec = {}
flag = '*'
recList = {}
cnt=0
def start_element(name, attrs):
sys.stdout.write(name+': ')
if 'orrfnbr'==name:
flag = 'ORRFNBR'
# sys.stdout.write(flag)
sys.stdout.flush()
def end_element(name):
sys.stdout.write('\n')
sys.stdout.flush()
def char_data(data):
sys.stdout.write(str(repr(data))[1:])
if flag == 'ORRFNBR':
rec['ORRFNBR']=str(repr(data))[1:]
sys.stdout.write(str(rec))
# recList['1']=rec
# cnt = cnt+1
# sys.stdout.write(str(recList[0]['X']))
sys.stdout.flush()
files = dircache.listdir('./xmls/')
for f in iter(files):
print f
g=open('./xmls/'+f, 'r')
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data
p.ParseFile(g)
print recList
g.close()
------
Here I'm trying to :
1. Read each XML file in a folder.
2. Parse file.
3. Store some of the tags values as key-value pair in a map
4. Similarly maintain another collection that'll store one map per file.
Please someone help me.
--
With Regards
Amitesh K.
9850638640
More information about the Expat-discuss
mailing list