[XML-SIG] my own entity defs when parsing with etree?

Stefan Behnel stefan_ml at behnel.de
Sun Jul 12 22:27:45 CEST 2009

Stuart McGraw wrote:
> I could use some really basic help about using Etree.
> I have tried reading the etree and expat doc but I
> don't understand most of it.

In that case, you should read up on XML in general first. The Wikipedia
article isn't all that bad:


> I have an xml file that contains a dtd that defines a 
> number of entities that are subsequently referenced 
> in the xml. 
> What I would like to do:
> 1) Parse the xml file but override some or all of the 
> entity definitions in the dtd with my own definitions.
> 2) Parse strings containing elements extracted from
> the full xml file, without the dtd, and supplying my 
> own entity map to resolve any entities.


> I am nearly clueless when it comes to xml processesing
> so if I could get a code snippet illustrating how to 
> do the above, that would be wonderful!  I am currently 
> using the stock Python 2.6 elementTree, but could 
> switch to lxml's if that would help.

ElementTree (i.e. the xml.etree package) does not supports DTDs at all. If
you want to use DTDs, e.g. to do validation, to inject default attributes,
or to resolve entity references, you can switch to the external lxml.etree
package. Note, however, that lxml does not support the ".entity" dictionary
on parsers. It doesn't currently have a way to override entity definitions
outside of a DTD.


More information about the XML-SIG mailing list