Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an ExpatError?

Tags:

python

xml

I've been playing around with parsing XML with Python, and I've found that making a spelling mistake in my XML tags raises an ExpatError. Since I don't think my code is residing in a foreign country, to what does an ExpatError refer, in general?

Misspelled Code:

minidom.parseString("<people><pesron>Dan</person><person>John</person></people>")

Results In:

ExpatError                                Traceback (most recent call last)
<ipython-input-5-9c00296c48cb> in <module>()
----> 1 minidom.parseString("<people><pesron>Dan</person><person>John</person></people>")

/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/xml/dom/minidom.pyc in parseString(string, parser)
   1928     if parser is None:
   1929         from xml.dom import expatbuilder
-> 1930         return expatbuilder.parseString(string)
   1931     else:
   1932         from xml.dom import pulldom

/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/xml/dom/expatbuilder.pyc in parseString(string, namespaces)
    938     else:
    939         builder = ExpatBuilder()
--> 940     return builder.parseString(string)
    941 
    942 

/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/xml/dom/expatbuilder.pyc in parseString(self, string)
    221         parser = self.getParser()
    222         try:
--> 223             parser.Parse(string, True)
    224             self._setup_subset(string)
    225         except ParseEscape:

ExpatError: mismatched tag: line 1, column 21
like image 371
Dan Avatar asked Nov 04 '22 01:11

Dan


1 Answers

Summing up and expanding on the comments to the question:

ExpatError is the type of exception raised when expat reports an error. expat is the Python Standard Library's XML parsing module.

minidom, Python's minimal implementation of the Document Object Model interface, uses expat internally to parse the XML input when minidom.parseString() is called.

The typo in the input XML left it with a <pesron> tag that is not closed, triggering expat to indicate this by throwing the ExpatError.

As to the origin of the name expat, rather than being a reference to "expatriate" in English, it is a shortened version of the module's description: (E)Xml PArser Toolkit.

like image 190
urig Avatar answered Nov 15 '22 06:11

urig