什么是DOM?

文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展置标语言的标准编程接口。

一个 DOM 的解析器在解析一个 XML 文档时,一次性读取整个文档,把文档中所有元素保存在内存中的一个树结构里,之后你可以利用DOM 提供的不同的函数来读取或修改文档的内容和结构,也可以把修改过的内容写入xml文件。

优点:操作简单,容易理解

缺点:因DOM需要将XML数据映射到内存中的树,一是比较慢,二是比较耗内存

movies.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>

xmltest.py:解析movies.xml文件的python代码如下:

# -*- coding:UTF-8 -*-

'''
Created on 2015年9月10日 @author: xiaowenhui
''' from xml.dom.minidom import parse
import xml.dom.minidom #第一种方法,DOM解析 #使用minidom解析器打开xml文档
DOMTree = xml.dom.minidom.parse("movies.xml")
collection = DOMTree.documentElement #在集合中获取所有电影
movies = collection.getElementsByTagName("movie") #打印每部电影的详细信息
dict_movies = {} for movie in movies:
dict_movie = {}
title = ""
print "*****Movie*****"
if movie.hasAttribute("title"): #具有属性
print "Title:%s" % movie.getAttribute("title") #获取属性值
title = movie.getAttribute("title") try:
type = movie.getElementsByTagName("type")[0]
print "Type :%s" % type.childNodes[0].data
dict_movie["type"] = type.childNodes[0].data format = movie.getElementsByTagName("format")[0] #获取该标签下的第一个子节点
print "format:%s" % format.childNodes[0].data
dict_movie["format"] = format.childNodes[0].data try:
year = movie.getElementsByTagName("year")[0]
print "year :%s" % year.childNodes[0].data
dict_movie["year"] = year.childNodes[0].data
except:
pass try:
episodes = movie.getElementsByTagName("episodes")[0]
print "episodes:%s" % episodes.childNodes[0].data
dict_movie["episodes"] = episodes.childNodes[0].data
except:
pass rating = movie.getElementsByTagName('rating')[0]
print "Rating: %s" % rating.childNodes[0].data
dict_movie["rating"] = rating.childNodes[0].data stars = movie.getElementsByTagName('stars')[0]
print "stars: %s" % stars.childNodes[0].data
dict_movie["stars"] = stars.childNodes[0].data description = movie.getElementsByTagName('description')[0]
print "Description: %s" % description.childNodes[0].data
dict_movie["description"] = description.childNodes[0].data
except:
print "error:" + title + "\n"
continue dict_movies[title] = dict_movie print dict_movies

解析后的输出结果如下:

*****Movie*****
Title:Enemy Behind
Type :War, Thriller
format:DVD
year :2003
Rating: PG
stars: 10
Description: Talk about a US-Japan war
*****Movie*****
Title:Transformers
Type :Anime, Science Fiction
format:DVD
year :1989
Rating: R
stars: 8
Description: A schientific fiction
*****Movie*****
Title:Trigun
Type :Anime, Action
format:DVD
episodes:4
Rating: PG
stars: 10
Description: Vash the Stampede!
*****Movie*****
Title:Ishtar
Type :Comedy
format:VHS
Rating: PG
stars: 2
Description: Viewable boredom
{u'Transformers': {'rating': u'R', 'description': u'A schientific fiction', 'format': u'DVD', 'stars': u'', 'year': u'', 'type': u'Anime, Science Fiction'}, u'Ishtar': {'rating': u'PG', 'type': u'Comedy', 'description': u'Viewable boredom', 'stars': u'', 'format': u'VHS'}, u'Enemy Behind': {'rating': u'PG', 'description': u'Talk about a US-Japan war', 'format': u'DVD', 'stars': u'', 'year': u'', 'type': u'War, Thriller'}, u'Trigun': {'rating': u'PG', 'description': u'Vash the Stampede!', 'format': u'DVD', 'episodes': u'', 'stars': u'', 'type': u'Anime, Action'}}

最新文章

  1. [linux系统]--crontab定时任务
  2. Asp.net Mvc模块化开发之分区扩展框架
  3. Socket编程基础——无连接UDP
  4. java hashMap缓存简单实现
  5. python中字符与ascii码转换
  6. C#导出数据到Excel通用的方法类
  7. an error occured during the file system check
  8. 启用 ASP.NET MVC 项目的 Edit and Continue
  9. 201521123011 《Java程序设计》 第三周学习总结
  10. 06jQuery-01-基本选择器
  11. yum安装指定版本的软件包的方法
  12. 【Never Stop】联赛集训记录
  13. windows安装node和yarn
  14. vue插件大全汇总
  15. VIP之CVI CVO
  16. Entity Framework 6 (7) vs NHibernate 4: DDD perspective(纯净DDD很难很难...)
  17. bzoj4035【HAOI2015】数组游戏
  18. Python数据采集处理分析挖掘可视化应用实例
  19. Docker实战:更轻松、更愉快、更高效
  20. IOS Masonry自动布局

热门文章

  1. 关于iscroll.js插件的使用
  2. jQuery基本选择器模块
  3. Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
  4. android黑科技系列——破解游戏之修改金币数
  5. MySQL 5.6 Reference Manual-14.5 InnoDB Tablespace Management
  6. (转)ORA-01502
  7. RabbitMQ学习之spring配置文件rabbit标签的使用
  8. public var tap: ControlEvent
  9. 优动漫PAINT之绘画助手软件简介
  10. IE 11 浏览器兼容性视图设置