Python解析xml文件(一)
2016-08-09 14:49
357 查看
解析xml文件:
思路:
一、首先获取需要解析的文档的
二、获取解析的文档中的所有的节点元素
三、根据需要调用相关的API获取相应的信息
Python中的xml.dom.minidom模块是用来处理xml文件的,所以在处理xml文件的时候需要把这个模块引入进来
首先写一个xml文件存储一下:
获取标签属性:
思路:
一、首先获取需要解析的文档的
二、获取解析的文档中的所有的节点元素
三、根据需要调用相关的API获取相应的信息
Python中的xml.dom.minidom模块是用来处理xml文件的,所以在处理xml文件的时候需要把这个模块引入进来
首先写一个xml文件存储一下:
<collection shelf="New Arrivals"> <movie title="Enemy Behind"> <type>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>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 import xml.dom.minidom # 使用minidom解析器打开 XML 文档 DOMTree = xml.dom.minidom.parse("a.xml") #获取文件中的节点元素 collection = DOMTree.documentElement #nodeName节点的名字 print collection.nodeName #nodeValue是结点的值,只对文本结点有效 print collection.nodeValue #nodeType是结点的类型 print collection.nodeType #ELEMENT_NODE节点的类型之一 print collection.ELEMENT_NODE
相关文章推荐
- Python 解析XML文件
- 用python解析xml文件
- Python解析XML文件
- python3解析XML文件
- Python解析xml文件遇到的编码解析的问题
- 用python来解析xml文件(简单情况)
- python解析xml文件实例分享
- python解析xml文件实例分享
- 使用python3.4解析xml文件(sax、dom、etree)
- Python SAX模块对大xml文件解析的错误认识
- python语言解析xml文件
- python 利用lxml 解析xml文件
- 用python来解析xml文件(简单情况)
- 解析XML文件总结 分类: python基础学习 python 2013-06-17 12:04 232人阅读 评论(0) 收藏
- python 学习 (二) 解析xml文件
- 利用python的xmllib2实现XML文件解析
- Python 解析XML文件
- python解析GBK格式xml文件
- Python 解析xml文件
- Python-elementTree方法解析xml文件-01