1.准备样例文件website.xml:

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

<p>Hi, there. My name is Mr. Yang. 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>Shouting Page of Mr. yang</h1>

<p>...</p>
    </page>
    <page name="sleeping" title="Sleeping">
      <h1>Sleeping Page of Mr. yang</h1>

<p>...</p>
    </page>
    <page name="eating" title="Eating">
      <h1>Eating Page of Mr. yang</h1>

<p>...</p>
    </page>
  </directory>
</website>
2. 简单的实现,编写python脚本xml_1.py:

#! /usr/bin/env python2.6
# Written by Tony.yang

#
from xml.sax import parse
from xml.sax.handler import ContentHandler

class PageMaker(ContentHandler):
        passthrough = False
        def startElement(self, name, attrs):
                if name == 'page':
                        self.passthrough = True
                        self.out = open(attrs['name'] + '.html', 'w')
                        self.out.write('<html><head>\n')
                        self.out.write('<title>%s</title>\n' % attrs['title'])
                        self.out.write('</head><body>\n')
                elif self.passthrough:
                        self.out.write('<' + name)
                        for key, val in attrs.items():
                                self.out.write('%s="%s"' % (key, val))
                        self.out.write('>')

def endElement(self, name):
                if name == 'page':
                        self.passthrough = False
                        self.out.write('\n</body></html>\n')
                        self.out.close()
                elif self.passthrough:
                        self.out.write('</%s>' % name)

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

parse('website.xml', PageMaker())

3. 优化后的脚本:(添加了一些其他功能):

xml_2.py:

#! /usr/bin/env python2.6

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, 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):
                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.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'))

转载于:https://blog.51cto.com/3403658/1109442

python-object-twoxml-html_1相关推荐

  1. Python maximum recursion depth exceeded while calling a Python object (gevent的SSL无限递归错误)的问题解决

    报错信息 源码位置 分析 很尴尬,完全看不出原因导致这个报错 解决方法 通过删除代码的方式一部一部删除,找到了问题出处 原因是包的顺序出现了问题,把位置互换一下,发现没有报错了,但是很明确的告诉你这两 ...

  2. 安装sikuli报错:jnius/jnius_conversion.pxi:54:31: Casting temporary Python object to non-numeric non-Pyth

    问题:ubuntu 安装sikuli报错:jnius/jnius_conversion.pxi:54:31: Casting temporary Python object to non-numeri ...

  3. python object类型是什么_python object是何种类型

    python object是何种类型 发布时间:2020-08-26 10:23:01 来源:亿速云 阅读:58 作者:Leah 这篇文章运用简单易懂的例子给大家介绍python object是何种类 ...

  4. python object has no attribute_如何修复python中的“AttributeError:type object has no attribute”?...

    您的代码引发此异常: AttributeError: type object 'Meeting' has no attribute 'datetime' 在这一行: meeting_start = M ...

  5. python object类_Python中一切皆对象,这个对象究竟是什么?

    点击上方蓝字CGRnDStudio关注我们" CG TD编程技术相关领域自媒体 " 作者:古明地盆 https://www.cnblogs.com/traditional/p/13 ...

  6. python object和type的关系-Python 的 type 和 object 之间是怎么一种关系?

    class,metaclass,instance,subclass,base 以下成立: 对任意的A,A是instance(推论:任意class也是instance) 对任意A,存在B,使得B是A的c ...

  7. python object的实例是什么_python中的type和object详解

    这篇博客主要描述Python的新风格对象(new-style objects),如下: 和分别是什么? 用户自定义的类和实例之间是怎么联系的?它们和内置类型又是怎么联系的? 什么是元类(metacla ...

  8. python Object Oriented Programming

    python 知识点整理(五) 本文只是对python部分知识点进行学习和整理 本篇主要是针对python的Object Oriented Programming的总结 本文目录 python 知识点 ...

  9. python object 类

    1. 在Python3之前,Python有两种类:旧式类和新式类.新式类必须从类object继承,否则就是旧式类. Python3以后统一新式类,所以也就不需要object类 2. 只有class C ...

  10. Python object()函数

    描述: Object类是Python中所有类的基类,如果定义一个类时没有指定继承哪个类,则默认继承object类. object没有定义__dict__,所以不能对object类实例对象尝试设置属性. ...

最新文章

  1. 一个学术 导航网站----科塔学术
  2. (筆記) 如何使用Visual C++ 6.0開發Win32 DLL? (C/C++) (VC++)
  3. python之sys模块【获取参数】
  4. 二维数组最大关联子数组
  5. 丰收互联蓝牙key怎么开机_ublox收购Rigado的蓝牙模块业务,扩大蓝牙低功耗产品组合...
  6. [笔记]读.Net 2.0面向对像揭密--条件编译
  7. php pdf打印横向,如何将pdfFactory打印调整为横向 - 应用技巧 - 常青藤软件工作室...
  8. 网站微信扫码登录实现步骤
  9. 简介计算机桌面功能,desktopcal电脑桌面日历功能介绍
  10. 随笔 2021-11-23
  11. java开发设置用户头像_如何修改 WordPress 的用户默认头像?
  12. jquery实现字数限制,超过部分...代替,后缀点击展开,点击后展开全文
  13. [week15] C - ZJM与纸条(选做)—— KMP算法
  14. 【Mysql笔试】-常见笔试题汇总
  15. EJB到底是什么?(推荐)
  16. Pygame键盘输入和鼠标操作
  17. Cadence学习记录(三)芯片封装设计
  18. CSRF——跨站请求伪造攻击
  19. 1gb.ru php,BeGet.ru:俄罗斯1G容量稳定PHP免费空间申请使用图文教程 | 骤雨打新荷...
  20. 第三次工业革命(三)

热门文章

  1. python开发讲解_Python开发系列课程(1) - 初识Python详解
  2. 串口输出换行_stm32初学者必会操作----usart串口调试工具
  3. java 数字运算异常_Java基础之:异常及异常处理
  4. Wait Event Disk file operations I/O
  5. jQuery学习笔记(五)
  6. 插入排序:直接插入排序希尔排序
  7. 【转载】java InputStream读取数据问题
  8. ubuntu 在vm中如何上网及注意问题
  9. Greenplum-概念篇
  10. 如何用VS2005制作Web安装程序