Hi,
I've had a bit of experience with perl and shell scripting. I've been reading the O'Reilly Python book and have been having a go at replicating some of our perl scripts in python (v3.1.2).
I'm looking to put something together than will parse through large xml files and manipulate the results. I've been messing with "xml.parsers.expat" but I'm having problems getting the arguments out of the StartElementHandler, although the list prints the correct results, all I can return is the integer '1'.
Could someone have a look and see what I'm doing wrong?
$ cat xmlparse.py
#!/usr/bin/python
import xml.parsers.expat
# handler functions
def start_element(L, D):
print(L,type(L),D,type(D))
test = ([L,D])
print('The list "test:"')
print(test)
return test
# test xml string.
xmlstring = '<xml attr="1" ><text>hello</text></xml>'
#create the parser
parsed = []
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = start_element
parsed = p.Parse(xmlstring)
print ("\nThis is the returned info:")
print(parsed,type(parsed))
$ ./python31.exe xmlparse.py
xml <class 'str'> {'attr': '1'} <class 'dict'>
The list "test:"
['xml', {'attr': '1'}]
text <class 'str'> {} <class 'dict'>
The list "test:"
['text', {}]
This is the returned info:
1 <class 'int'>
$
I admin, I'm sort of aheadof my reading so if I'm doing something fundamentally wrong, I'd appreciate a shove in the right direction :)