Python function xml.dom.minidom.parse () ignores DTD

I have the following Python code:

import xml.dom.minidom
import xml.parsers.expat

try:
    domTree = ml.dom.minidom.parse(myXMLFileName)
except xml.parsers.expat.ExpatError, e:
    return e.args[0]

which I use to parse an XML file. Although it pretty happily detects simple XML errors, such as inappropriate tags, it completely ignores the DTD specified at the top of the XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE ServerConfig SYSTEM "ServerConfig.dtd">

therefore, he does not notice when, for example, the required elements are missing. How to enable DTD verification?

+3
source share
5 answers

See this question - accepted answer - use lxml validation .

+4
source

: Python xml.dom.minidom xml.sax expat, . DTD, , DTD.

gimel Tim lxml, libxml2 libxslt. DTD. lxml, .

+3

, :

from lxml import etree

try:
    parser = etree.XMLParser(dtd_validation=True)
    domTree = etree.parse(myXMLFileName, parser=parser)
except etree.XMLSyntaxError, e:
    return e.args[0]
+2

lxml xmlproc, PyXML ( xmlproc) ; Python, PyXML, 2.4.

+1
0

All Articles