Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../Demo/xml
File: rss2html.py
"""
[0] Fix | Delete
A demo that reads in an RSS XML document and emits an HTML file containing
[1] Fix | Delete
a list of the individual items in the feed.
[2] Fix | Delete
"""
[3] Fix | Delete
[4] Fix | Delete
import sys
[5] Fix | Delete
import codecs
[6] Fix | Delete
[7] Fix | Delete
from xml.sax import make_parser, handler
[8] Fix | Delete
[9] Fix | Delete
# --- Templates
[10] Fix | Delete
[11] Fix | Delete
top = """\
[12] Fix | Delete
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
[13] Fix | Delete
<html>
[14] Fix | Delete
<head>
[15] Fix | Delete
<title>%s</title>
[16] Fix | Delete
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
[17] Fix | Delete
</head>
[18] Fix | Delete
[19] Fix | Delete
<body>
[20] Fix | Delete
<h1>%s</h1>
[21] Fix | Delete
"""
[22] Fix | Delete
[23] Fix | Delete
bottom = """
[24] Fix | Delete
</ul>
[25] Fix | Delete
[26] Fix | Delete
<hr>
[27] Fix | Delete
<address>
[28] Fix | Delete
Converted to HTML by rss2html.py.
[29] Fix | Delete
</address>
[30] Fix | Delete
[31] Fix | Delete
</body>
[32] Fix | Delete
</html>
[33] Fix | Delete
"""
[34] Fix | Delete
[35] Fix | Delete
# --- The ContentHandler
[36] Fix | Delete
[37] Fix | Delete
class RSSHandler(handler.ContentHandler):
[38] Fix | Delete
[39] Fix | Delete
def __init__(self, out=sys.stdout):
[40] Fix | Delete
handler.ContentHandler.__init__(self)
[41] Fix | Delete
self._out = codecs.getwriter('utf-8')(out)
[42] Fix | Delete
[43] Fix | Delete
self._text = ""
[44] Fix | Delete
self._parent = None
[45] Fix | Delete
self._list_started = False
[46] Fix | Delete
self._title = None
[47] Fix | Delete
self._link = None
[48] Fix | Delete
self._descr = ""
[49] Fix | Delete
[50] Fix | Delete
# ContentHandler methods
[51] Fix | Delete
[52] Fix | Delete
def startElement(self, name, attrs):
[53] Fix | Delete
if name == "channel" or name == "image" or name == "item":
[54] Fix | Delete
self._parent = name
[55] Fix | Delete
[56] Fix | Delete
self._text = ""
[57] Fix | Delete
[58] Fix | Delete
def endElement(self, name):
[59] Fix | Delete
if self._parent == "channel":
[60] Fix | Delete
if name == "title":
[61] Fix | Delete
self._out.write(top % (self._text, self._text))
[62] Fix | Delete
elif name == "description":
[63] Fix | Delete
self._out.write("<p>%s</p>\n" % self._text)
[64] Fix | Delete
[65] Fix | Delete
elif self._parent == "item":
[66] Fix | Delete
if name == "title":
[67] Fix | Delete
self._title = self._text
[68] Fix | Delete
elif name == "link":
[69] Fix | Delete
self._link = self._text
[70] Fix | Delete
elif name == "description":
[71] Fix | Delete
self._descr = self._text
[72] Fix | Delete
elif name == "item":
[73] Fix | Delete
if not self._list_started:
[74] Fix | Delete
self._out.write("<ul>\n")
[75] Fix | Delete
self._list_started = True
[76] Fix | Delete
[77] Fix | Delete
self._out.write(' <li><a href="%s">%s</a> %s\n' %
[78] Fix | Delete
(self._link, self._title, self._descr))
[79] Fix | Delete
[80] Fix | Delete
self._title = None
[81] Fix | Delete
self._link = None
[82] Fix | Delete
self._descr = ""
[83] Fix | Delete
[84] Fix | Delete
if name == "rss":
[85] Fix | Delete
self._out.write(bottom)
[86] Fix | Delete
[87] Fix | Delete
def characters(self, content):
[88] Fix | Delete
self._text = self._text + content
[89] Fix | Delete
[90] Fix | Delete
# --- Main program
[91] Fix | Delete
[92] Fix | Delete
if __name__ == '__main__':
[93] Fix | Delete
parser = make_parser()
[94] Fix | Delete
parser.setContentHandler(RSSHandler())
[95] Fix | Delete
parser.parse(sys.argv[1])
[96] Fix | Delete
[97] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function