# -*- coding: utf-8 *- #!/usr/bin/env python import os from EntryDTO import * from xml.dom import minidom from xml.dom import XML_NAMESPACE class EntryDAO: def __init__(self): pass def load(self,file_name): self.listEntry = [] self.dom = minidom.parse(file_name) self.root = self._getNode(self.dom,'entries') fileName = os.path.basename(file_name) self.load_entries(fileName) def load_entries(self,fileName): def load_entry(entryNode): entri = Entry() kwd = self._getNode(entryNode, 'keyword') mod = self._getNode(entryNode, 'module') entri.keyword = self._getNodeText(kwd) entri.module = self._getNodeText(mod) entri.id = entryNode.getAttribute('id') listText = self._getChieldByTagName(entryNode, 'text') for texx in listText: tx = Text() tx.value = self._getNodeText(texx) tx.lang = texx.getAttributeNS(XML_NAMESPACE, 'lang') entri.listText.append(tx) return entri chlds = self.root.childNodes for chld in chlds: if chld.nodeType!=chld.ELEMENT_NODE: continue if chld.tagName == 'entry': entri = load_entry(chld) entri.messageFile = fileName self.listEntry.append(entri) def _getNode(self, parent_node, name): try: return parent_node.getElementsByTagName(name)[0] except: return None def _getNodeText(self,node): try: txt=node.childNodes[0].nodeValue except: txt='' return txt def _getChieldByTagName(self,parent_node, tag): result=[] chlds = parent_node.childNodes for chld in chlds: try: if chld.tagName == tag: result.append(chld) except: pass return result def _findNodeByAttribute(self,parent_node,tag,attribute,value): chlds = self._getChieldByTagName(parent_node,tag) for chld in chlds: try: if chld.getAttribute(attribute)==value: return chld except: pass return None if __name__=='__main__': entryDAO = EntryDAO() entryDAO.load('/Users/aboufahj/Documents/workspace/PDQPDSSimulator/PDQPDSSimulator-war/src/main/messages/messages-PDQPDSSimulator.xml') for entri in entryDAO.listEntry: print entri