zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    阶梯博弈
    hihoCoder #1199 : Tower Defense Game ——(树型dp)
    2016 China-Final-F题 ——(SA+二分)
    ACM之路(20)—— Splay初探
    2016 ICPC China-Final 现场赛总结
    【Interleaving String】cpp
    【Best Time to Buy and Sell Stock III 】cpp
    【Maximal Rectangle】cpp
    【palindrome partitioning II】cpp
    【Maximum Subarray 】cpp
  • 原文地址:https://www.cnblogs.com/pyfreshman/p/4594163.html
Copyright © 2011-2022 走看看