Python xml etree DTD from StringIO source?

I am adapting the following code (created using the advice of this question ) that took an XML file and its DTD and converted them to another format. For this task, only the loading section is important:

xmldoc = open(filename) parser = etree.XMLParser(dtd_validation=True, load_dtd=True) tree = etree.parse(xmldoc, parser) 

This worked fine, although it used a file system, but I convert it to run through a web framework where two files are uploaded via a form.

Downloading an XML file works fine:

 tree = etree.parse(StringIO(data['xml_file']) 

But since the DTD is associated with the top of the xml file, the following statement does not work:

 parser = etree.XMLParser(dtd_validation=True, load_dtd=True) tree = etree.parse(StringIO(data['xml_file'], parser) 

Through this question , I tried:

 etree.DTD(StringIO(data['dtd_file']) tree = etree.parse(StringIO(data['xml_file']) 

While the first line does not cause an error, the second falls on the unicode objects that the DTD is intended for selection (and does this in the file system version):

XMLSyntaxError: Entity 'eacute' not row 4495, column 46

How to load this DTD?

+4
source share
2 answers

Here's a short but complete example that uses the @Steven special recognition technique.

 from StringIO import StringIO from lxml import etree data = dict( xml_file = '''<?xml version="1.0"?> <!DOCTYPE x SYSTEM "a.dtd"> <x><y>&eacute;zz</y></x> ''', dtd_file = '''<!ENTITY eacute "&#233;"> <!ELEMENT x (y)> <!ELEMENT y (#PCDATA)> ''') class DTDResolver(etree.Resolver): def resolve(self, url, id, context): return self.resolve_string(data['dtd_file'], context) xmldoc = StringIO(data['xml_file']) parser = etree.XMLParser(dtd_validation=True, load_dtd=True) parser.resolvers.add(DTDResolver()) try: tree = etree.parse(xmldoc, parser) except etree.XMLSyntaxError as e: # handle xml and validation errors 
+5
source

Perhaps you can use a custom converter . The docs actually provide an example for this to provide dtd.

+1
source

All Articles