• xml解析,练习


    <collection shelf="New Arrivals">
    <movie title="Enemy Behind">
       <type hehe="laji">War, Thriller</type>
       <format>DVD</format>
       <year>2003</year>
       <rating>PG</rating>
       <stars>10</stars>
       <description>Talk about a US-Japan war</description>
    </movie>
    <movie title="Transformers">
       <type hehe="food">Anime, Science Fiction</type>
       <format>DVD</format>
       <year>1989</year>
       <rating>R</rating>
       <stars>8</stars>
       <description>A schientific fiction</description>
    </movie>
       <movie title="Trigun">
       <type>Anime, Action</type>
       <format>DVD</format>
       <episodes>4</episodes>
       <rating>PG</rating>
       <stars>10</stars>
       <description>Vash the Stampede!</description>
    </movie>
    <movie title="Ishtar">
       <type>Comedy</type>
       <format>VHS</format>
       <rating>PG</rating>
       <stars>2</stars>
       <description>Viewable boredom</description>
    </movie>
    </collection>

    # -*- coding: utf-8 -*- from xml.dom.minidom import parse

    #使用minidom解析器打开 XML 文档 docObj = parse("moves.xml") root = docObj.documentElement if root.hasAttribute("shelf"):     rootAttribute = root.getAttribute("shelf")

    print(rootAttribute)

    # 在集合中获取所有电影 movies = root.getElementsByTagName("movie")

    for movie in movies:     print("-"*20)     if movie.hasAttribute("title"):         print(movie.getAttribute("title"))     typeMovie = movie.getElementsByTagName("type")[0]

        if typeMovie.hasAttribute("hehe"):         print(typeMovie.getAttribute("hehe"))     print(typeMovie.childNodes[0].data)

  • 相关阅读:
    线程池的创建方式
    lock和synchronized如何选择?
    Java中常用的url签名防篡改方法
    jvm异常记录
    mysql索引
    PHP:PDO prepare预处理
    零度CC JavaScript获取页面、屏幕尺寸大小
    【转】 Javascript中document.execCommand()的用法
    获取元素的属性-border问题
    CSS样式定义的优先级顺序总结
  • 原文地址:https://www.cnblogs.com/pyfreshman/p/4594163.html
Copyright © 2020-2023  润新知