How to resolve external objects using xml.etree, e.g. lxml.etree

I have a script that parses XML using lxml.etree:

from lxml import etree

parser = etree.XMLParser(load_dtd=True, resolve_entities=True)
tree = etree.parse('main.xml', parser=parser)

I need load_dtd=Trueand resolve_entities=Truehave &emptyEntry;of globals.xmlallowed:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE map SYSTEM "globals.xml" [
    <!ENTITY dirData "${DATADIR}"> 
]>
<map 
    xmlns:map="http://my.dummy.org/map"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsschemaLocation="http://my.dummy.org/map main.xsd"
>

  &emptyEntry; <!-- from globals.xml -->

  <entry><key>KEY</key><value>VALUE</value></entry>
  <entry><key>KEY</key><value>VALUE</value></entry>
</map>

with globals.xml

<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY emptyEntry "<entry></entry>">

Now I need to switch from non-standard lxmlto standard xml.etree. But it can not be with my file, because load_dtd=True, and resolve_entities=Truenot supported xml.etree.

Is there a xml.etree-way to resolve these objects?

+5
source share
2 answers

lxml is the right tool for the job.

stdlib, XMLParser UseForeignDTD. ( ) : Python ElementTree XML?

0

xmllint

proc = subprocess.Popen(['xmllint','--noent',fname],stdout=subprocess.PIPE)
output = proc.communicate()[0]
tree = ElementTree.parse(StringIO.StringIO(output))
+1

All Articles