def testGetTextAndAttributes_03(self): testXML = """ <Crisis ID="CRI_BAGAIR" Name="Baghdad Airstrike"> <People> <Person ID="PER_BRAMAN" /> <Person ID="PER_JULASS" /> </People> <Organizations> <Org ID="ORG_WIKLKS" /> </Organizations> <Kind>War</Kind> <Date>2007-07-12</Date> <Locations> <li>New Baghdad, Baghdad, Iraq</li> </Locations> <HumanImpact> <li>12 civilians died, 2 children wounded</li> </HumanImpact> <Common> <ExternalLinks> <li href="https://en.wikipedia.org/wiki/July_12,_2007_Baghdad_airstrike">Wikipedia</li> </ExternalLinks> </Common> </Crisis> """ # Remove tabs and newlines from testXML testXML = " ".join(testXML.split()) tree = ET.fromstring(testXML) it = tree.iter() e = it.next() pairs = getTextAndAttributes(e) self.assert_(len(pairs) == 2) self.assert_( pairs["ID"] == "CRI_BAGAIR" ) self.assert_( pairs["Name"] == "Baghdad Airstrike" ) # Advance iterator for i in range(14): e = it.next() try: it.next() raise Exception("StopIteration exception should have been raised!") except StopIteration as e: pass
def testGetTextAndAttributes_02(self): testXML = """ <Person ID="PER_OSAMAB" Name="Osama Bin Laden"> <Crises> <Crisis ID="CRI_NINEOO" /> </Crises> <Organizations> <Org ID="ORG_AQAEDA" /> </Organizations> <Kind>Founder of al-Qaeda</Kind> <Common> <ExternalLinks> <li href="https://en.wikipedia.org/wiki/Osama_Bin_Laden">Wikipedia</li> </ExternalLinks> <Images> <li embed="https://upload.wikimedia.org/wikipedia/commons/c/ca/Osama_bin_Laden_portrait.jpg" /> </Images> </Common> </Person> """ # Remove tabs and newlines from testXML testXML = " ".join(testXML.split()) tree = ET.fromstring(testXML) it = tree.iter() e = it.next() pairs = getTextAndAttributes(e) self.assert_(len(pairs) == 2) self.assert_( pairs["ID"] == "PER_OSAMAB" ) self.assert_( pairs["Name"] == "Osama Bin Laden" ) # Advance iterator for i in range(10): e = it.next() try: it.next() raise Exception("StopIteration exception should have been raised!") except StopIteration as e: pass
def testGetTextAndAttributes_01(self): testXML = """ <element attribute1="a" attribute2="b" attribute3="c" attribute4="d">12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</element> """ tree = ET.fromstring(testXML) it = tree.iter() pairs = getTextAndAttributes( it.next() ) self.assert_(len(pairs) == 5) self.assert_( pairs["attribute1"] == "a" ) self.assert_( pairs["attribute2"] == "b" ) self.assert_( pairs["attribute3"] == "c" ) self.assert_( pairs["attribute4"] == "d" ) self.assert_( pairs["content"] == "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" ) try: it.next() raise Exception("StopIteration exception should have been raised!") except StopIteration as e: pass