Sax parses the dd.xml into HTML. Of course, if you get the XSL file for XML, you can convert it to HTML directly with LIBXML2.
Copy the Code code as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
#---------------------------------------
# Program: XML Parser
# version: 01.0
# Author: Mupeng
# Date: 2013-12-18
# language: Python 2.7
# function: Parse XML into corresponding HTML
# Note: The program parses the XML with the parse function of the Xml.sax module and generates an event
# Inherit ContentHandler and override its event handler function
# Dispatcher is mainly used to distribute the starting and ending events of the corresponding labels.
#---------------------------------------
From Xml.sax.handler import ContentHandler
From Xml.sax Import Parse
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 ()
def startelement (self, Name, attrs):
Self.dispatch (' Start ', name, attrs)
def endElement (self, name):
Self.dispatch (' End ', name)
Class Website (Dispatcher, ContentHandler):
def __init__ (self):
self.fout = open (' ddt_sax.html ', ' W ')
Self.imagein = False
Self.desflag = False
Self.item = False
Self.title = ' '
Self.link = ' '
Self.guid = ' '
Self.url = ' '
Self.pubdate = ' '
self.description = ' '
Self.temp = ' '
Self.prx = ' '
def startchannel (self):
Self.fout.write ("'\ n\ n rss-") <br><br> def endchannel (self):<br> self.fout.write (" ' <BR> &L T;TR><TD height= "></td></tr><BR> </table><BR> </center><BR> <BR> </body><BR> </ptm L><br> ") <BR> Self.fout.close () </P><P> def characters (self, chars): &L t;br> if Chars.strip ():<br> #chars = Chars.strip () <BR> self.temp + = Chars<b R> #print self.temp<br><br> <BR> def starttitle (self):<br><br> If self.item:<br> self.fout.write ("<BR> <tr bgcolor=" #eeeeee ">\n< TD style= "PADDING-TOP:5PX;PADDING-LEFT:5PX;" height= ">\n<B><BR>") <br><br > Def endtitle (self):<br><br> if not Self.imagein and not self.item:<br> self.title = Self.temp<br> ; Self.temp = "<BR> self.fout.write (Self.title.encode (' gb2312 ')) <br><br> #self. titl E = self.temp<br> self.fout.write ("<BR>\ n\ n\ n
\ n
\ n
\ n
\ n
\ n
''')
If Self.item:
Self.title = Self.temp
Self.temp = ' '
Self.fout.write (Self.title.encode (' gb2312 '))
Self.fout.write ("'
|
''')
def startimage (self):
Self.imagein = True
def endimage (self):
Self.imagein = False
def startlink (self):
If Self.imagein:
Self.fout.write ("'
def endlink (self):
Self.link = Self.temp
Self.temp = ' '
If Self.imagein:
Self.fout.write (Self.link.encode (' gb2312 '))
Self.fout.write ("target=" _blank ">\n")
Elif Self.item:
#self. link = self.temp
Pass
Else
Self.fout.write (Self.link)
Self.fout.write ("target=")
_blank
">")
Self.fout.write (Self.title.encode (' gb2312 '))
Self.fout.write ("' |
''')
Self.fout.write (Self.description.encode (' gb2312 '))
Self.fout.write ("'
|
| ¸´öæ´ëò³á´½óîòòªç¶èë¸ãðâîåáð±íµ½îòµäò³ã棨¼òµ¥¡¢¿ìëù¡¢êµê±¡¢ãâ Ñ£© |
def starturl (self):
If Self.imagein:
Self.fout.write ("Def Endurl (self):
Self.url = Self.temp
Self.temp = ' '
If Self.imagein:
Self.fout.write (Self.url.encode (' gb2312 '))
Self.fout.write ("" "border=" 0 ">\n
#程序入口
if __name__ = = ' __main__ ':
Parse (' Ddt.xml ', Website ())
''')
")
If Self.item:
#self. url = Self.temp
Pass
def defaultstart (self):
Pass
def defaultend (self):
Self.temp = '
def STARTD Escription:
Pass
def enddescription (self):
Self.description = self.temp
Self.temp = "
if self. Item:
#self. Fout.write (' ¡¡¡¡ ')
Self.fout.write (Self.description.encode (' gb2312 '))
def endguid (self ):
Self.guid = self.temp
def endpubdate (self):
If not self.temp.startswith (' http '):
Self.pubdate = self.t EMP
Self.temp = '
Else:
Self.pubdate = '
def startItem (self):
Self.item = True
def enditem (self) :
Self.item = False
Self.fout.write ("'
|
Self.fout.write (Self.link)
Self.fout.write ("target=" _blank ">")
Self.fout.write (Self.guid)
Self.fout.write ("'
|
''')
Self.fout.write (Self.pubdate)
Self.fout.write ("' |
|
''')