Validating an XML File with LXML
I’ve been playing with XML files recently and have on the odd occasion needed to validate a file against an XML schema. This is surprisingly easy using lxml, the Swiss Army knife of Python XML processing. Allow me to demonstrate.
>>> from lxml import etree
>>> schema = etree.XMLSchema(etree.parse('schema_file_name.xsd'))
>>> xml_file = etree.parse('xml_file_name.xml')
>>> schema.validate(xml_file)
True
Job done. If you are unlucky enough that your file doesn’t validate you can find out by checking the error_log attribute of your XMLSchema object.
It is possible to verify an XML documaint against an XSD in a ‘streaming’ fashion? That is without loading/parsing the entire document into memory at once?
Comment by Adam Tauno Williams — 22/08/2011 @ 8:54 am
It is possible to verify an XML documaint against an XSD in a ‘streaming’ fashion? That is without loading/parsing the entire document into memory at once?
Comment by Adam Tauno Williams — 22/08/2011 @ 8:54 am
please fix small typo: s/entree/etree/ and then remove this comment
Comment by Piotr Ożarowski — 29/08/2011 @ 7:48 pm
please fix small typo: s/entree/etree/ and then remove this comment
Comment by Piotr Ożarowski — 29/08/2011 @ 7:48 pm
Thanks, done.
Comment by Andy Todd — 06/09/2011 @ 9:15 pm
Thanks, done.
Comment by Andy Todd — 06/09/2011 @ 9:15 pm
Can you show a small sample parsing the XML data into MySQL using lxml? I am having a tough time!
Comment by Ray — 07/09/2011 @ 8:02 am
Can you show a small sample parsing the XML data into MySQL using lxml? I am having a tough time!
Comment by Ray — 07/09/2011 @ 8:02 am