cpython/Doc/includes/minidom-example.py

65 lines
1.5 KiB
Python
Raw Normal View History

2007-08-15 11:28:22 -03:00
import xml.dom.minidom
document = """\
<slideshow>
<title>Demo slideshow</title>
<slide><title>Slide title</title>
<point>This is a demo</point>
<point>Of a program for processing slides</point>
</slide>
<slide><title>Another demo slide</title>
<point>It is important</point>
<point>To have more than</point>
<point>one slide</point>
</slide>
</slideshow>
"""
dom = xml.dom.minidom.parseString(document)
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
def handleSlideshow(slideshow):
2007-08-30 15:50:25 -03:00
print("<html>")
2007-08-15 11:28:22 -03:00
handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])
slides = slideshow.getElementsByTagName("slide")
handleToc(slides)
handleSlides(slides)
2007-08-30 15:50:25 -03:00
print("</html>")
2007-08-15 11:28:22 -03:00
def handleSlides(slides):
for slide in slides:
handleSlide(slide)
def handleSlide(slide):
handleSlideTitle(slide.getElementsByTagName("title")[0])
handlePoints(slide.getElementsByTagName("point"))
def handleSlideshowTitle(title):
2007-08-30 15:50:25 -03:00
print("<title>%s</title>" % getText(title.childNodes))
2007-08-15 11:28:22 -03:00
def handleSlideTitle(title):
2007-08-30 15:50:25 -03:00
print("<h2>%s</h2>" % getText(title.childNodes))
2007-08-15 11:28:22 -03:00
def handlePoints(points):
2007-08-30 15:50:25 -03:00
print("<ul>")
2007-08-15 11:28:22 -03:00
for point in points:
handlePoint(point)
2007-08-30 15:50:25 -03:00
print("</ul>")
2007-08-15 11:28:22 -03:00
def handlePoint(point):
2007-08-30 15:50:25 -03:00
print("<li>%s</li>" % getText(point.childNodes))
2007-08-15 11:28:22 -03:00
def handleToc(slides):
for slide in slides:
title = slide.getElementsByTagName("title")[0]
2007-08-30 15:50:25 -03:00
print("<p>%s</p>" % getText(title.childNodes))
2007-08-15 11:28:22 -03:00
handleSlideshow(dom)