19th August, 2011

Validating an XML File with LXML

Filed under: python — admin @ 8:35 pm

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.

 

8 Comments

  1. 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

  2. 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

  3. please fix small typo: s/entree/etree/ and then remove this comment :)

    Comment by Piotr Ożarowski — 29/08/2011 @ 7:48 pm

  4. please fix small typo: s/entree/etree/ and then remove this comment :)

    Comment by Piotr Ożarowski — 29/08/2011 @ 7:48 pm

  5. Thanks, done.

    Comment by Andy Todd — 06/09/2011 @ 9:15 pm

  6. Thanks, done.

    Comment by Andy Todd — 06/09/2011 @ 9:15 pm

  7. 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

  8. 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

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress