Exemple #1
0
 def test_iterparse_xpath(self):
     with open(self.path, 'rb') as f:
         elements = list(drill.iterparse(f, xpath='catalog/magazine'))
         self.assertEqual(len(elements), 1)
         # Make sure children of matched elements are still parsed correctly.
         self.assertEqual([e.tagname for e in elements[0]], ['author', 'title', 'price', 'book'])
     with open(self.path, 'rb') as f:
         elements = list(drill.iterparse(f, xpath='catalog/*/title'))
         self.assertEqual(len(elements), 3)
         self.assertEqual([e.data for e in elements], ['Test Book', u('Él Libro'), 'Test Magazine'])
Exemple #2
0
 def test_custom_handler_class(self):
     doc = drill.parse(self.path, handler_class=CustomHandler)
     self.assertEqual(doc[0].__class__, CustomElement)
     for e in drill.iterparse(open(self.path, 'rb'),
                              handler_class=CustomHandler):
         self.assertEqual(e.__class__, CustomElement)
         e.clear()
Exemple #3
0
 def test_iterparse(self):
     parsed_tags = []
     for e in drill.iterparse(open(self.path, 'rb')):
         parsed_tags.append(e.tagname)
         e.clear()
     self.assertEqual(
         parsed_tags,
         [
             'author',
             'isbn',
             'title',
             'book',  # The first book, children first
             'author',
             'isbn',
             'title',
             'extra_element',
             'book',  # The second book
             'author',
             'title',
             'price',  # Magazine elements
             'title',
             'isbn',
             'book',  # Book inside the magazine
             'magazine',  # The magazine
             'catalog'  # Finally, the root catalog
         ])
     # The last element should be the finished root element, and it should be empty (since we cleared as we parsed).
     self.assertEqual(e.tagname, 'catalog')
     self.assertEqual(len(e), 0)
Exemple #4
0
 def test_iterparse(self):
     parsed_tags = []
     for e in drill.iterparse(open(self.path, 'rb')):
         parsed_tags.append(e.tagname)
         e.clear()
     self.assertEqual(parsed_tags, [
         'author', 'isbn', 'title', 'book',  # The first book, children first
         'author', 'isbn', 'title', 'extra_element', 'book',  # The second book
         'author', 'title', 'price',  # Magazine elements
         'title', 'isbn', 'book',  # Book inside the magazine
         'magazine',  # The magazine
         'catalog'  # Finally, the root catalog
     ])
     # The last element should be the finished root element, and it should be empty (since we cleared as we parsed).
     self.assertEqual(e.tagname, 'catalog')
     self.assertEqual(len(e), 0)
Exemple #5
0
 def test_custom_handler_class(self):
     doc = drill.parse(self.path, handler_class=CustomHandler)
     self.assertEqual(doc[0].__class__, CustomElement)
     for e in drill.iterparse(open(self.path, 'rb'), handler_class=CustomHandler):
         self.assertEqual(e.__class__, CustomElement)
         e.clear()