Advertisement
mixster

mixster

Dec 26th, 2010
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import xml.parsers.expat
  3. import urllib
  4.  
  5. class Parser(object):
  6.   children = []
  7.   data = ''
  8.  
  9.   def __init__(self, name='root', par=None, pas=None):
  10.     self.name = name
  11.     self.parent = par
  12.     self.parser = pas
  13.  
  14.   def parse(self, data):
  15.     print 'Here'
  16.     self.parser = xml.parsers.expat.ParserCreate()
  17.     self.parser.StartElementHandler = self.newElement
  18.     self.parser.EndElementHandler = self.endElement
  19.     self.parser.CharacterDataHandler = self.newData
  20.     self.parser.Parse(data)
  21.  
  22.   def newElement(self, name, attrs):
  23.     print 'There'
  24.     self.children.append(Parser(name, self, self.parser))
  25.     self.parser.StartElementHandler = self.children[-1].newElement
  26.     self.parser.EndElementHandler = self.children[-1].endElement
  27.     self.parser.CharacterDataHandler = self.children[-1].newData
  28.  
  29.   def endElement(self, name):
  30.     print 'Everywhere'
  31.     self.parser.StartElementHandler = self.parent.newElement
  32.     self.parser.EndElementHandler = self.parent.endElement
  33.     self.parser.CharacterDataHandler = self.parent.newData
  34.  
  35.   def newData(self, data):
  36.     self.data = data
  37.  
  38. f = urllib.urlopen('data')
  39. s = ''
  40. for line in f:
  41.   s += line
  42. f.close()
  43.  
  44. p = Parser()
  45. p.parse(s)
  46.  
  47. stack = [('', p)]
  48.  
  49. while stack:
  50.   t = stack.pop(0)
  51.   for c in t[1].children:
  52.     stack.append((t[0] + t[1].name + '.', c))
  53.   print t[0] + t[1].name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement