zoukankan      html  css  js  c++  java
  • python xml DOM 解析例子

    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.append(node.data)
        return ''.join(rc)
    
    def handleSlideshow(slideshow):
        print "<html>"
        handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])
        slides = slideshow.getElementsByTagName("slide")
        handleToc(slides)
        handleSlides(slides)
        print "</html>"
    
    def handleSlides(slides):
        for slide in slides:
            handleSlide(slide)
    
    def handleSlide(slide):
        handleSlideTitle(slide.getElementsByTagName("title")[0])
        handlePoints(slide.getElementsByTagName("point"))
    
    def handleSlideshowTitle(title):
        print "<title>%s</title>" % getText(title.childNodes)
    
    def handleSlideTitle(title):
        print "<h2>%s</h2>" % getText(title.childNodes)
    
    def handlePoints(points):
        print "<ul>"
        for point in points:
            handlePoint(point)
        print "</ul>"
    
    def handlePoint(point):
        print "<li>%s</li>" % getText(point.childNodes)
    
    def handleToc(slides):
        for slide in slides:
            title = slide.getElementsByTagName("title")[0]
            print "<p>%s</p>" % getText(title.childNodes)
    
    handleSlideshow(dom)

    运行结果:

    <html>
    <title>Demo slideshow</title>
    <p>Slide title</p>
    <p>Another demo slide</p>
    <h2>Slide title</h2>
    <ul>
    <li>This is a demo</li>
    <li>Of a program for processing slides</li>
    </ul>
    <h2>Another demo slide</h2>
    <ul>
    <li>It is important</li>
    <li>To have more than</li>
    <li>one slide</li>
    </ul>
    </html>

  • 相关阅读:
    [nodejs]npm国内npm安装nodejs modules终极解决方案
    [nodejs]解决mysql和连接池(pool)自动断开问题
    [nodejs]国内npm安装nodejs modules失败的几个解决方案
    [less]用webstorm自动编译less产出css和sourcemap
    [javascript] Promise API
    [javascript]巧用sourcemap快速定位javascript中的问题
    Gruntjs提高生产力(四)
    Gruntjs提高生产力(三)
    Gruntjs提高生产力(二)
    Gruntjs提高生产力(一)
  • 原文地址:https://www.cnblogs.com/hzhida/p/2655768.html
Copyright © 2011-2022 走看看