PageRenderTime 335ms CodeModel.GetById 201ms app.highlight 6ms RepoModel.GetById 125ms app.codeStats 0ms

/Doc/includes/minidom-example.py

http://unladen-swallow.googlecode.com/
Python | 64 lines | 63 code | 1 blank | 0 comment | 0 complexity | 82cddf85569484594d61425d4e91b806 MD5 | raw file
 1import xml.dom.minidom
 2
 3document = """\
 4<slideshow>
 5<title>Demo slideshow</title>
 6<slide><title>Slide title</title>
 7<point>This is a demo</point>
 8<point>Of a program for processing slides</point>
 9</slide>
10
11<slide><title>Another demo slide</title>
12<point>It is important</point>
13<point>To have more than</point>
14<point>one slide</point>
15</slide>
16</slideshow>
17"""
18
19dom = xml.dom.minidom.parseString(document)
20
21def getText(nodelist):
22    rc = ""
23    for node in nodelist:
24        if node.nodeType == node.TEXT_NODE:
25            rc = rc + node.data
26    return rc
27
28def handleSlideshow(slideshow):
29    print "<html>"
30    handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])
31    slides = slideshow.getElementsByTagName("slide")
32    handleToc(slides)
33    handleSlides(slides)
34    print "</html>"
35
36def handleSlides(slides):
37    for slide in slides:
38        handleSlide(slide)
39
40def handleSlide(slide):
41    handleSlideTitle(slide.getElementsByTagName("title")[0])
42    handlePoints(slide.getElementsByTagName("point"))
43
44def handleSlideshowTitle(title):
45    print "<title>%s</title>" % getText(title.childNodes)
46
47def handleSlideTitle(title):
48    print "<h2>%s</h2>" % getText(title.childNodes)
49
50def handlePoints(points):
51    print "<ul>"
52    for point in points:
53        handlePoint(point)
54    print "</ul>"
55
56def handlePoint(point):
57    print "<li>%s</li>" % getText(point.childNodes)
58
59def handleToc(slides):
60    for slide in slides:
61        title = slide.getElementsByTagName("title")[0]
62        print "<p>%s</p>" % getText(title.childNodes)
63
64handleSlideshow(dom)