1

I have been searching for the answer to this for days. All of the examples of creating custom XML parsers that I can find (e.g. in the docs, or this example, or this question or this question) talk about generating entirely new data (e.g. the depth of the XML, or a CSV equivalent).

However, all I want to do is intercept the parsing, inspect the data, possibly alter it, then let the parser continue.

I tried this, as a toy example:

from xml.etree import ElementTree as ET

class myParser():
  def start(self, tag, data):
    ##### Pretty sure this is wrong, but what should it be? #####
    return ET.XMLParser.start(tag, data)
  def data(self, data):
    return ET.XMLParser.data(data.replace('"', '"'))
  def end(self, tag):
    return ET.XMLParser.end(tag)
  def close(self):
    return ET.XMLParser.close()

def parseFile(fileName):
  p = ET.XMLParser(target=myParser)
  tree = ET.parse(fileName, parser=p)

but I'm getting

TypeError: 'unbound method start() must be called with myParser instance as first argument (got str instance instead)'

I feel like I'm close, but I'm missing some vital piece of the puzzle that I just can't see.

Community
  • 1
  • 1
Wilson F
  • 1,250
  • 1
  • 20
  • 37
  • You have defined the `start()` method twice in that class. You should change the name of one of them. – coralvanda Jun 22 '16 at 08:21
  • Oops, that second one was meant to be `close()`. Copy-paste error : ( I'm not at that computer, so I'll have to try that when I get there this evening. – Wilson F Jun 22 '16 at 16:51
  • I fixed that problem (and another typo) and am still experiencing the same error. – Wilson F Jun 22 '16 at 17:01

0 Answers0