python编程金典 pdf_[转]《Python编程金典》读书笔记

Example 16.2. 一个XML论坛的例子

Feedback

python

my first forum

this is a xml forum.

--- 用于表示一个论坛的XML文档(内含一篇文章)---

===============================================================================

Feedback

--- 显示所有论坛的XML文档(内含一个feedback论坛) ---

===============================================================================

#!c:\python23\python.exe

# filename: default.py

# Default page for message forums

import os

import sys

from xml.dom.ext.reader import PyExpat

def printHeader( title, style ):

print """Content-type: text/html

/p>

"-//W3C//DTD XHTML 1.0 Strict//EN"

"DTD/xhtml-strict.dtd">

%s

# open XML document that contains the forum names and locations

try:

XMLFile = open( "../htdocs/XML/forums.xml" )

except IOError:

print "Location: /error.html\n"

sys.exit()

# parse XML document containing forum information

reader = PyExpat.Reader()

document = reader.fromStream( XMLFile )

XMLFile.close()

# write XHTML to browser

printHeader( "my Forum", "/XML/site.css" )

print """

my Forum

Available Forum

  • """

# determine client-browser type

if os.environ[ "HTTP_USER_AGENT"].find( "MSIE" ) != -1:

prefix = "../XML/"

else:

prefix = "forum.py?file="

# add links for each forum

for forum in document.getElementsByTagName( "forum" ):

#create link to forum

link = prefix + forum.attributes.item( 0 ).value

#get element nodes containing tag name "name"

name = forum.getElementsByTagName( "name" )[ 0 ]

#get Text node's value

nameText = name.childNodes[ 0 ].nodeValue

print '

%s' % ( link, nameText )

print """

Forum Management

"""

reader.releaseNode( document )

--- 论坛的默认页面 ---

===============================================================================

#!c:\python23\python.exe

# filename: addForum.py

# Adds a forum to the list

import re

import sys

import cgi

#4DOM packages

from xml.dom.ext.reader import PyExpat

from xml.dom.ext import PrettyPrint

def printHeader( title, style ):

print """Content-type: text/html

/p>

"-//W3C//DTD XHTML 1.0 Strict//EN"

"DTD/xhtml-strict.dtd">

%s

form = cgi.FieldStorage()

# if user enters data in form fields

if form.has_key( "name" ) and form.has_key( "filename" ):

newFile = form[ "filename" ].value

#determine whether file has xml extension

if not re.match( "\w+\.xml$", newFile ):

print "Location: /error.html\n"

sys.exit()

else:

# create forum files from xml files

try:

newForumFile = open( "../htdocs/XML/" + newFile, "w" )

forumsFile = open( "../htdocs/XML/forums.xml", "r+" )

templateFile = open ( "../htdocs/XML/template.xml" )

except IOError:

print "Location: /error.html\n"

sys.exit()

# parse forums document

reader = PyExpat.Reader()

document = reader.fromStream( forumsFile )

#add new forum element

forum = document.createElement( "forum" )

forum.setAttribute( "filename", newFile )

name = document.createElement( "name" )

nameText = document.createTextNode( form[ "name" ].value )

name.appendChild( nameText )

forum.appendChild( name )

# obtain root element of forum

documentNode = document.documentElement

firstForum = documentNode.getElementsByTagName( "forum" )[ 0 ]

documentNode.insertBefore( forum, firstForum )

# write update XML to disk

forumsFile.seek(0, 0)

forumsFile.truncate()

PrettyPrint( document, forumsFile )

forumsFile.close()

# create document for new forum from template file

document = reader.fromStream( templateFile )

forum = document.documentElement

forum.setAttribute( "file", newFile )

# create name element

name = document.createElement( "name" )

nameText = document.createTextNode( form[ "name" ].value )

name.appendChild( nameText )

forum.appendChild( name )

# write generated XML to new forum file

PrettyPrint( document, newForumFile )

newForumFile.close()

templateFile.close()

reader.releaseNode( document )

print "Location: default.py\n"

else:

printHeader( "Add a forum", "/XML/site.css" )

print """

Forum Name

Forum File Name

Return to Main Page.

"""

--- 向forums.xml添加新论坛的脚本 ---

===============================================================================

---用于生成新论坛的XML模板 ---

===============================================================================

#!c:\python23\python.exe

# Adds a message to a forum

import re

import os

import sys

import cgi

import time

#4DOM packages

from xml.dom.ext.reader import PyExpat

from xml.dom.ext import PrettyPrint

def printHeader( title, style ):

print """Content-type: text/html

/p>

"-//W3C//DTD XHTML 1.0 Strict//EN"

"DTD/xhtml-strict.dtd">

%s

# identify client browser

if os.environ[ "HTTP_USER_AGENT" ].find( "MSIE" ) != -1:

prefix = "../XML/"#Internet Explorer

else:

prefix = "forum.py?file="

form = cgi.FieldStorage()

#user has submitted message to post

if form.has_key( "submit" ):

filename = form[ "file" ].value

# add message to forum

if not re.match( "\w+\.xml$", filename ):

print "Location: /error.html\n"

sys.exit()

try:

forumFile = open( "../htdocs/XML/" + filename, "r+" )

except IOError:

print "Location: /error.html\n"

sys.exit()

# parse forum document

reader = PyExpat.Reader()

document = reader.fromStream( forumFile )

documentNode = document.documentElement

# create message element

message = document.createElement( "message" )

message.setAttribute( "timestamp", time.ctime( time.time() ) )

# add elements to message

messageElements = [ "user", "title", "text" ]

for item in messageElements:

if not form.has_key( item ):

text = "( Field left blank )"

else:

text = form[ item ].value

# create nodes

element = document.createElement( item )

elementText = document.createTextNode( text )

element.appendChild( elementText )

message.appendChild( element )

#append new message to forum and update document on disk

documentNode.appendChild( message )

forumFile.seek(0,0)

forumFile.truncate()

PrettyPrint( document, forumFile )

forumFile.close()

reader.releaseNode( document )

print "Location: %s\n" % ( prefix + form[ "file" ].value )

# create form to obtain new message

elif form.has_key( "file" ):

printHeader( "Add a posting", "/XML/site.css" )

print """\n

User

Message Title

Message Text

Return to Forum

""" % ( form[ "file" ].value, prefix + form[ "file" ].value )

else:

print "Location: /error.html\n"

--- 为论坛添加文章的脚本 ---

===============================================================================

xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

../cgi-bin/addPost.py?file=

Post a Message

Return to Main Page

by

at

--- 将XML转换成XHTML的XSLT样式表 ---

===============================================================================

#!c:\python23\python.exe

#display forum postings for non-Internet Explorer browser.

import re

import cgi

import sys

from xml.xslt import Processor

form = cgi.FieldStorage()

# form to display has been specified

if form.has_key( "file" ):

# determine whether file is xml

if not re.match( "\w+\.xml$", form[ "file" ].value ):

print "Location: /error.html\n"

sys.exit()

try:

style = open( "../htdocs/XML/formatting.xsl" )

XMLFile = open( "../htdocs/XML/" + form[ "file" ].value )

except IOError:

print "Location: /error.html\n"

sys.exit()

# create XSLT processor instance

processor = Processor.Processor()

# specify style sheet

processor.appendStylesheetStream( style )

# apply style sheet to XML document

results = processor.runStream( XMLFile )

style.close()

XMLFile.close()

print "Content-type: text/html\n"

print results

else:

print "Location: /error.html\n"

--- 为不支持XSLT的浏览器将XML转换成HTML ---

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值