Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import xml.parsers.expat
- import urllib
- class Parser(object):
- children = []
- data = ''
- def __init__(self, name='root', par=None, pas=None):
- self.name = name
- self.parent = par
- self.parser = pas
- def parse(self, data):
- print 'Here'
- self.parser = xml.parsers.expat.ParserCreate()
- self.parser.StartElementHandler = self.newElement
- self.parser.EndElementHandler = self.endElement
- self.parser.CharacterDataHandler = self.newData
- self.parser.Parse(data)
- def newElement(self, name, attrs):
- print 'There'
- self.children.append(Parser(name, self, self.parser))
- self.parser.StartElementHandler = self.children[-1].newElement
- self.parser.EndElementHandler = self.children[-1].endElement
- self.parser.CharacterDataHandler = self.children[-1].newData
- def endElement(self, name):
- print 'Everywhere'
- self.parser.StartElementHandler = self.parent.newElement
- self.parser.EndElementHandler = self.parent.endElement
- self.parser.CharacterDataHandler = self.parent.newData
- def newData(self, data):
- self.data = data
- f = urllib.urlopen('data')
- s = ''
- for line in f:
- s += line
- f.close()
- p = Parser()
- p.parse(s)
- stack = [('', p)]
- while stack:
- t = stack.pop(0)
- for c in t[1].children:
- stack.append((t[0] + t[1].name + '.', c))
- print t[0] + t[1].name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement