python处理xml文件

文章介绍了如何使用Python的xml.dom.minidom模块解析movies.xml文件,提取并显示每部电影的类型、格式、评级和描述等信息。
摘要由CSDN通过智能技术生成

1、新建一个movies.xml文件

<collection shelf="New Arrivals">
<movie title="Enemy Behind">
   <type>War, Thriller</type>
   <type>War, Thriller1</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>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>

2、python代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from xml.dom.minidom import parse
import xml.dom.minidom

# 使用minidom解析器打开 XML 文档
DOMTree = xml.dom.minidom.parse("movies.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("shelf"):
    print("Root element : %s" % collection.getAttribute("shelf"))
# 在集合中获取所有电影
movies = collection.getElementsByTagName("movie")

# 打印每部电影的详细信息
for movie in movies:
    print("*****Movie*****")
    if movie.hasAttribute("title"):
        print("Title: %s" % movie.getAttribute("title"))
    print(movie.getElementsByTagName('type'))
    type = movie.getElementsByTagName('type')[0]
    print(type.childNodes[0])
    print("Type: %s" % type.childNodes[0].data)

    format = movie.getElementsByTagName('format')[0]
    print("Format: %s" % format.childNodes[0].data)

    rating = movie.getElementsByTagName('rating')[0]
    print("Rating: %s" % rating.childNodes[0].data)

    description = movie.getElementsByTagName('description')[0]
    print("Description: %s" % description.childNodes[0].data)

3、结论:

C:\Users\Thinkpad\AppData\Local\Programs\Python\Python39\python.exe C:/Users/Thinkpad/Desktop/解析xml文件/解析.py 
Root element : New Arrivals
*****Movie*****
Title: Enemy Behind
[<DOM Element: type at 0x207d8ef73a0>, <DOM Element: type at 0x207d8ef7430>]
<DOM Text node "'War, Thril'...">
Type: War, Thriller
Format: DVD
Rating: PG
Description: Talk about a US-Japan war
*****Movie*****
Title: Transformers
[<DOM Element: type at 0x207d8ef7820>]
<DOM Text node "'Anime, Sci'...">
Type: Anime, Science Fiction
Format: DVD
Rating: R
Description: A schientific fiction
*****Movie*****
Title: Trigun
[<DOM Element: type at 0x207d8ef7c10>]
<DOM Text node "'Anime, Act'...">
Type: Anime, Action
Format: DVD
Rating: PG
Description: Vash the Stampede!
*****Movie*****
Title: Ishtar
[<DOM Element: type at 0x207d8eff040>]
<DOM Text node "'Comedy'">
Type: Comedy
Format: VHS
Rating: PG
Description: Viewable boredom

Process finished with exit code 0

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值