Parse XML file

"amitesh kumar" <[email protected]>
Newsgroups gmane.text.xml.expat.general
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.