def testParseWithElementStringResult(self): xmlString = _ElementStringResult("""<tag><content>contents</content></tag>""") self.observable.do.add(identifier="id", partname="partName", data=xmlString) self.assertEquals(1, len(self.observer.calledMethods)) self.assertEquals("add", self.observer.calledMethods[0].name) self.assertEquals("id", self.observer.calledMethods[0].kwargs['identifier']) self.assertEquals("partName", self.observer.calledMethods[0].kwargs['partname']) xmlNode = self.observer.calledMethods[0].kwargs['lxmlNode'] rootTag = xmlNode.getroot() self.assertEquals('tag', rootTag.tag) self.assertEquals(['content'], [c.tag for c in rootTag.getchildren()])
def gettagvalue(tag, function=None): """Gets tag text, attribute, or tail, depending on the xpath NOTE: This seems to work with namespaced attributes, while settagvalue does not. """ value = None if function: value = function(tag) else: if type(tag) == type(etree._ElementStringResult()): value = tag elif hasattr(tag, 'text'): value = tag.text elif type(tag) == type(''): value = tag elif tagtype(tag) == 'attribute': attr = f['xpath'].split('@')[1] value = tag.getparent().attrib[attr] # strip before/after whitespace try: value = value.strip() except: pass return value
def __init__(self, element): BaseElementWrapper.__init__(self, element) # if element is None then we use a bogus xml element as to not throw an error when using partial. # this can lead to an exception (AttributeError: '_ElementStringResult' object has no attribute 'xpath') # when using an item plugin which wasn't requested. Ex creating a parser class using the Images plugin # without sending Images in the ResponseGroups param. self.xpath = functools.partial(self.element.xpath, namespaces=self.namespaces) if element is not None else lambda x: [etree._ElementStringResult()]