def parsexml(self,xml): log.debug('in parsexml') # 如果传进来的是文件,则直接读取内容 # 如果是字符串,则先尝试解释,如果不是xml则再尝试作为文件路径,再不行,则抛异常了。 xml_content = '' if type(xml) == file: xml_content = xml.read() xml.close() elif type(xml) == str or type(xml) == unicode: log.debug('try to load file') if os.path.exists(xml): xml_file = open(xml,'r') xml_content = xml_file.read() xml_file.close() else: xml_content = xml else: log.error('could not init testcase from xml') raise TypeError,'fromxml need a file instance or a string instance for argument' log.debug('starting parse xml') log.debug('xml content: %s'%xml_content) doc = minidom.parseString(xml_content) ret = get_child_tags(doc,'test')#this statement return a list log.debug('child tag len : %d'%len(ret)) if ret: test_node = ret[0] return test_node else: log.warn('no test node in the xml!')
def parse_child_tags(self,xml): """ """ for child_node in get_child_tags(xml): # 1.把未定义的标签转换成TestNode,当作当前结点的属性 if get_registed_node(child_node.tagName): comp = get_registed_node(child_node.tagName)(name=child_node.tagName,parent=self) else: comp = TestNode(name=child_node.tagName,parent=self) # 这里可以把不需特别处理的Tag处理成当前节点的属性 comp.fromxml(child_node) self._children.append(comp) # 所有Child放在一个List self._child_tags.add(child_node.tagName) #self._add_or_append_attr(child_node.tagName,comp) # 标签名相同的Child放在一个List # 2.看是否有相应的_parse方法提供,有则调用,作特别处理 if getattr(self,'_parse_%s'%child_node.tagName,None): # 如果是可解释的元素,则亲自解释,如attr getattr(self,'_parse_%s'%child_node.tagName)(child_node)
def parse_child_tags(self, xml): """ """ for child_node in get_child_tags(xml): # 1.把未定义的标签转换成TestNode,当作当前结点的属性 if get_registed_node(child_node.tagName): comp = get_registed_node(child_node.tagName)( name=child_node.tagName, parent=self) else: comp = TestNode(name=child_node.tagName, parent=self) # 这里可以把不需特别处理的Tag处理成当前节点的属性 comp.fromxml(child_node) self._children.append(comp) # 所有Child放在一个List self._child_tags.add(child_node.tagName) #self._add_or_append_attr(child_node.tagName,comp) # 标签名相同的Child放在一个List # 2.看是否有相应的_parse方法提供,有则调用,作特别处理 if getattr(self, '_parse_%s' % child_node.tagName, None): # 如果是可解释的元素,则亲自解释,如attr getattr(self, '_parse_%s' % child_node.tagName)(child_node)