سؤال

With lxml I can:

from lxml import etree
parser = etree.XMLParser(resolve_entities=False)

Can I do the same with xml.etree.ElementTree XMLParser?

هل كانت مفيدة؟

المحلول

A quick look at the source code shows nothing as easy as resolve_entities=False.

Here is an extract of the source of xml.etree.ElementTree.XMLParser:

parser.DefaultHandlerExpand = self._default
parser.StartElementHandler = self._start
parser.EndElementHandler = self._end
parser.CharacterDataHandler = self._data
# optional callbacks
parser.CommentHandler = self._comment
parser.ProcessingInstructionHandler = self._pi

There are no other pyexpat handlers configured.

Now, are you interested in well-known XML entities like & or others? It seems like undefined entities are going through XMLParser._default so you might get something by extending that method.

But what are you trying to accomplish with this? If this is only adding new entities, try updating the XMLParser.entity dict.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top