Example 16.2. 一个XML论坛的例子
Feedback
python
my first forumthis 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
- Add a Forum
- Delete a Forum
- Modify a Forum
"""
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">
%sform = 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
"""
--- 向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
""" % ( form[ "file" ].value, prefix + form[ "file" ].value )
else:
print "Location: /error.html\n"
--- 为论坛添加文章的脚本 ---
===============================================================================
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
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 ---