Return an object after parsing xml with SAX
I have some large XML files to parse and have created an object class to contain my relevant data. Unfortunately, I am unsure how to return the object for later processing. Right now I pickle my data and moments later depickle the object for access. This seems wasteful, and there surely must be a way of grabbing my data without hitting the disk.
def endElement(self, name):
if name == "info": # done collecting this iteration
self.data.setX(self.x)
self.data.setY(self.y)
elif name == "lastTagOfInterest": # done with file
# want to return my object from here
filehandler = open(self.outputname + ".pi", "w")
pickle.dump(self.data, filehandler)
filehandler.close()
I have tried putting a retu开发者_开发知识库rn statement in my endElement tag, but that does not seem to get passed up the chain to where I call the SAX parser.
Thanks for any tips.
Bah, sat and thought about it for a second and the answer was obvious. Return quit the method, and then just pull out the data field from the ContentHandler object I had created.
精彩评论