I working on xml sax parser to parse xml files and below is my code
xml file code:
Registered Nurse-Epilepsy&
You need to implement a characters handler too:
def characters(self, content):
print content
but this potentially gives you text in chunks instead of as one block per tag.
Do yourself a big favour though and use the ElementTree API instead; that API is far pythononic and easier to use than the XML DOM API.
from xml.etree import ElementTree as ET
etree = ET.parse('/path/to/xml_file.xml')
jobtitle = etree.find('job/title').text
If all you want is a straight conversion to a dictionary, take a look at this handy ActiveState Python Cookbook recipe: Converting XML to dictionary and back. Note that it uses the ElementTree API as well.
If you have a set of existing elements you want to look for, just use these in the find()
method:
fieldnames = [
'title', 'job-code', 'detail-url', 'job-category', 'description',
'summary', 'posted-date', 'location', 'address', 'city', 'state',
'zip', 'country', 'company', 'name', 'url']
fields = {}
etree = ET.parse('/path/to/xml_file.xml')
for field in fieldnames:
elem = etree.find(field)
if field is not None and field.text is not None:
fields[field] = elem.text