当前位置:Gxlcms > Python > 使用python解析xml成对应的html示例分享

使用python解析xml成对应的html示例分享

时间:2021-07-01 10:21:17 帮助过:29人阅读

SAX将dd.xml解析成html。当然啦,如果得到了xml对应的xsl文件可以直接用libxml2将其转换成html。

代码如下:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#---------------------------------------
# 程序:XML解析器
# 版本:01.0
# 作者:mupeng
# 日期:2013-12-18
# 语言:Python 2.7
# 功能:将xml解析成对应的html
# 注解:该程序用xml.sax模块的parse函数解析XML,并生成事件
# 继承ContentHandler并重写其事件处理函数
# Dispatcher主要用于相应标签的起始、结束事件的派发
#---------------------------------------
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> <tr><td height="20"></td></tr><BR> </table><BR> </center><BR> <script><BR> function GetTimeDiff(str)<BR> {<BR> if(str == '')<BR> {<BR> return '';<BR> }</P> <P> var pubDate = new Date(str);<BR> var nowDate = new Date();<BR> var diffMilSeconds = nowDate.valueOf()-pubDate.valueOf();<BR> var days = diffMilSeconds/86400000;<BR> days = parseInt(days);</P> <P> diffMilSeconds = diffMilSeconds-(days*86400000);<BR> var hours = diffMilSeconds/3600000;<BR> hours = parseInt(hours);</P> <P> diffMilSeconds = diffMilSeconds-(hours*3600000);<BR> var minutes = diffMilSeconds/60000;<BR> minutes = parseInt(minutes);</P> <P> diffMilSeconds = diffMilSeconds-(minutes*60000);<BR> var seconds = diffMilSeconds/1000;<BR> seconds = parseInt(seconds);<br><br> var returnStr = "±±¾©·¢²¼Ê±¼ä£º" + pubDate.toLocaleString();</P> <P> if(days > 0)<BR> {<BR> returnStr = returnStr + " £¨¾àÀëÏÖÔÚ" + days + "Ìì" + hours + "Сʱ" + minutes + "·ÖÖÓ£©";<BR> }<BR> else if (hours > 0)<BR> {<BR> returnStr = returnStr + " £¨¾àÀëÏÖÔÚ" + hours + "Сʱ" + minutes + "·ÖÖÓ£©";<BR> }<BR> else if (minutes > 0)<BR> {<BR> returnStr = returnStr + " £¨¾àÀëÏÖÔÚ" + minutes + "·ÖÖÓ£©";<BR> }</P> <P> return returnStr;</P> <P> }</P> <P> function GetSpanText()<BR> {<BR> var pubDate;<BR> var pubDateArray;<BR> var spanArray = document.getElementsByTagName("span");</P> <P> for(var i = 0; i < spanArray.length; i++)<BR> {<BR> pubDate = spanArray[i].innerHTML;<BR> document.getElementsByTagName("span")[i].innerHTML = GetTimeDiff(pubDate); <BR> }<BR> }</P> <P> GetSpanText();<BR> </ script><BR> </body><BR> </html><BR> ''')<BR> self.fout.close()</P> <P> def characters(self, chars):<BR> if chars.strip():<BR> #chars = chars.strip()<BR> self.temp += chars<BR> #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="30">\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.title = self.temp<BR> self.fout.write('''<BR> \n\n\n

\n