fulmicoton fulmicoton - 2 years ago 102
Python Question

How do I validate xml against a DTD file in Python

I need to validate an XML string (and not a file)
against a DTD description file.

How can that be done in


Answer Source

Another good option is lxml's validation which I find quite pleasant to use.

A simple example taken from the lxml site:

from StringIO import StringIO

from lxml import etree

dtd = etree.DTD(StringIO("""<!ELEMENT foo EMPTY>"""))
root = etree.XML("<foo/>")
# True

root = etree.XML("<foo>bar</foo>")
# False
# <string>:1:0:ERROR:VALID:DTD_NOT_EMPTY: Element foo was declared EMPTY this one has content
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download