Tidy up xml parser file handling

This commit is contained in:
Nick Hall 2015-10-05 18:30:56 +01:00
parent c97a254188
commit 41c17c3190
3 changed files with 7 additions and 8 deletions

View File

@ -47,16 +47,15 @@ class LdsTemples(object):
self.__current_temple = ""
self.__tlist = []
lds_filename = os.path.join(DATA_DIR, "lds.xml")
lds_filename = os.path.expanduser(os.path.join(DATA_DIR, "lds.xml"))
try:
xml_file = open(os.path.expanduser(lds_filename), 'rb')
parser = ParserCreate()
parser.StartElementHandler = self.__start_element
parser.EndElementHandler = self.__end_element
parser.CharacterDataHandler = self.__characters
with open(lds_filename, 'rb') as xml_file:
parser.ParseFile(xml_file)
xml_file.close()
except Exception as msg:
LOG.error(str(msg))

View File

@ -138,14 +138,13 @@ class TipParser(object):
self.mylist = []
self.skip = False
xml_file = open(TIP_DATA, 'rb')
self.tlist = []
parser = ParserCreate()
parser.StartElementHandler = self.startElement
parser.EndElementHandler = self.endElement
parser.CharacterDataHandler = self.characters
with open(TIP_DATA, 'rb') as xml_file:
parser.ParseFile(xml_file)
xml_file.close()
def get(self):
"""

View File

@ -325,7 +325,8 @@ class _Xml2Obj:
parser.StartElementHandler = self.start_element
parser.EndElementHandler = self.end_element
# Parse the XML File
parser.Parse(open(filename, 'rb').read(), 1)
with open(filename, 'rb') as xml_file:
parser.ParseFile(xml_file)
return self.root
#------------------------------------------------------------------------