Python基础教程项目3:万能的XML

  • website.py

# coding = utf-8
__author__ = 'twocold'


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)
        if not os.path.isdir(path): os.makedirs(path)

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

    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):
        self.directory.pop()

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

    def endPage(self):
        self.passthrough = False
        self.weiteFooter()
        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'))

  • website.xml

<website>
    <page name="index" title="Home Page">
        <h1>Welcome to My Home Page</h1>

        <p>Hi, there. My name is Mr.Gumby, and this is my home page. Here
        are some of my interests:</p>

        <ul>
            <li><a href="interests/shouting.html">Shouting</a></li>
            <li><a href="interests/sleeping.html">Sleeping</a></li>
            <li><a href="interests/eating.html">Eating</a></li>
        </ul>
    </page>
    <directory name="interests">
        <page name="shouting" title="Shouting">
            <h1>Mr. Gumby's Shouting Page</h1>

            <p>...</p>
        </page>
        <page name="sleeping" title="Sleeping">
            <h1>Mr. Gmby's Sleeping Page</h1>

            <p>...</p>
        </page>
        <page name="eating" title="Eating">
            <h1>Mr .Gumby's Eating Page</h1>

            <p>...</p>
        </page>
    </directory>
</website>


转载于:https://my.oschina.net/478744551/blog/511954

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值