免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1358 | 回复: 0
打印 上一主题 下一主题

python项目3 万能的xml中的疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-12-16 11:40 |只看该作者 |倒序浏览
from xml.sax.handler import ContentHandler
from xml.sax import parse
import os

class Dispatcher:
        def dispatch(self, prefix, name, attrs=None):
                mname = prefix + name.capitalize()
                dname = 'default' + prefix.capitalize()
                method = getattr(self, mname, None)
                if callable(method): args = ()
                else:
                        method = getattr(self, dname, None)
                        args = name,
                if prefix == 'start': args += attrs,
                if callable(method): method(*args)

        def startElement(self, name, attrs):
                self.dispatch('start', name, attrs)

        def endElement(self, name):
                self.dispatch('end', name)

class WebsiteConstructor(Dispatcher, ContentHandler):
        passthrough = False

        def __init__(self, directory):
                self.directory = [directory]
                self.ensureDirectory()

        def ensureDirectory(self):
                path = os.path.join(*self.directory)
                print path
                print '----'
                if not os.path.isdir(path): os.makedirs(path)

        def characters(self, chars):
                if self.passthrough: self.out.write(chars)

        def defaultStart(self, name, attrs):
                if self.passthrough:
                        self.out.write('<' + name)
                        for key, val in attrs.items():
                                self.out.write(' %s="%s"' %(key, val))
                        self.out.write('>')
        def defaultEnd(self, name):
                if self.passthrough:
                        self.out.write('</%s>' % name)

        def startDirectory(self, attrs):
                self.directory.append(attrs['name'])
                self.ensureDirectory()

        def endDirectory(self):
                print 'endDirectory'
                self.directory.pop()

        def startPage(self, attrs):
                print 'startPage'
                filename = os.path.join(*self.directory + [attrs['name']+'.html'])
                self.out = open(filename, 'w')
                self.writeHeader(attrs['title'])
                self.passthrough = True

        def endPage(self):
                print 'endPage'
                self.passthrough = False
                self.writeFooter()
                self.out.close()

        def writeHeader(self, title):
                self.out.write('<html>\n <head>\n   <title>')
                self.out.write(title)
                self.out.write('</title>\n </head>\n  <body>\n')

        def writeFooter(self):
                self.out.write('\n </body>\n</html>\n')

parse('website.xml',WebsiteConstructor('public_html'))


流程大概就是
1.首先创建一个public_html的文件,存放整个网站,然后读xml的节点,通过startElement和endElement调用dispatch进行处理。
2.然后就是dispatch怎么调用具体的处理函数了

小弟的疑问就是:
是因为startElement和endElement函数在前面,所以传递进来的html标签以及xml节点就会先被这两个函数调用,而不会被下面的函数直接调用吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP