Reputation: 3581
I am using lxml package (etree) to take in a xml schema and parse it against xml file using the code.
from lxml import etree
import traceback
schema_file = 'C:/Users/Romi/Desktop/XML Testing/schema.xsd'
def validate(xmlparser, xmlfilename):
try:
with open(xmlfilename, 'r') as f:
etree.fromstring(f.read(), xmlparser)
return True
except:
return False
with open(schema_file, 'r') as f:
schema_root = etree.XML(f.read())
schema = etree.XMLSchema(schema_root)
xmlparser = etree.XMLParser(schema=schema)
filenames = ['C:/Users/Romi/Desktop/XML Testing/feed.xml','C:/Users/Romi/Desktop/XML Testing/feed1.xml' ]
fo = open("C:/Users/Romi/Desktop/XML Testing/result.txt", "r+")
for filename in filenames:
if validate(xmlparser, filename):
print "%s validates with the schema." % filename
#fo.write("%s validates with the schema." % filename)
else:
print "%s doesn't validate with the schema." % filename
#fo.write("%s doesn't validate with the schema." % filename)
I am printing error when it doesnt validates but I want to print the entire traceback which points to where it failed exactly give the error and move on to next file for validation.
Any pointers?
Upvotes: 1
Views: 1164
Reputation: 5884
You can use the traceback library to print out the stack trace in your exception catching:
http://docs.python.org/2/library/traceback.html#traceback-examples
By the way, it's a good practice to limit your exception handling. I'd change it so it only catches lxml parsing errors - your validate() function will return False if open() fails, for example.
Something along the line of this:
try:
with open(xmlfilename, 'r') as f:
return etree.fromstring(f.read(), xmlparser)
except etree.XMLSyntaxError:
print traceback.format_exc()
Hope that helps!
Upvotes: 3