demo:

import xml.dom.minidom

dom=xml.dom.minidom.parse('sample.xml')
root = dom.documentElement
cc=dom.getElementsByTagName('movie')
c1=cc[0]
print(root.nodeName)
print(root.nodeValue)
print(root.nodeType)
print(root.ELEMENT_NODE)

aa=root.getElementsByTagName('movie')
a=aa[0]
print('***************************')
print(a.nodeName)
print(a.nodeValue)
print(a.getAttribute('title'))
print(a.firstChild)
print(len(aa))
print(a.getAttribute('title'))
print(a.getElementsByTagName('type')[0].childNodes[0].data)
print(a.getElementsByTagName('format')[0].childNodes[0].data)

''' Parse XML using DOM '''
# coding:utf-8

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

DT = xml.dom.minidom.parse('sample.xml')
COLLECTION = DT.documentElement
if COLLECTION.hasAttribute('shelf'):
print('Root element : %s' % COLLECTION.getAttribute('shelf'))
# Get all films and print detail information
MOVIES = COLLECTION.getElementsByTagName('movie')
# 打印每部电影的详细信息
for movie in MOVIES:
type_ = movie.getElementsByTagName('type')[0]
format_ = movie.getElementsByTagName('format')[0]
rating = movie.getElementsByTagName('rating')[0]
description = movie.getElementsByTagName('description')[0]
print('*****Movie*****')
print('\tTitle: %s' % movie.getAttribute('title'))
print('\tType: %s' % type_.childNodes[0].data)
print('\tFormat: %s' % format_.childNodes[0].data)
print('\tRating: %s' % rating.childNodes[0].data)
print('\tDescription: %s' % description.childNodes[0].data)

python parse xml using DOM的更多相关文章

  1. python 解析xml 文件: DOM 方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  2. 【Python Network】使用DOM生成XML

    单纯的为DOM树添加结点. #!/usr/bin/env python # Generating XML with DOM - Chapter 8 - domgensample.py from xml ...

  3. python 解析XML python模块xml.dom解析xml实例代码

    分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...

  4. python 应用xml.dom.minidom读xml

    xml文件 <?xml version="1.0" encoding="utf-8"?> <city> <name>上海&l ...

  5. 递归方式 DOM 解析(parse) XML

    friends.xml <span style="font-size:16px;"><?xml version="1.0" encoding= ...

  6. Python读取xml报错解析--ExpatError: not well-formed (invalid token)

    xml文件内容如代码所示存入的名字为login.xml: <?xml version="1.0" encoding="utf-8"?> <in ...

  7. python读取xml文件

    关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...

  8. Python存取XML方法简介

    <?xml version="1.0" encoding="utf-8"?> <Schools> <School Name=&qu ...

  9. python解析xml模块封装代码

    在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...

随机推荐

  1. 由苹果的低级Bug想到的

    2014年2月22日,在这个“这么二”的日子里,苹果公司推送了 iOS 7.0.6(版本号11B651)修复了 SSL 连接验证的一个 bug.官方网页在这里:http://support.apple ...

  2. app-framework学习--官网地址及demo下载地址

    一起学习共同进步,加油..! 官网地址:http://app-framework-software.intel.com/ 下载地址:http://download.csdn.net/detail/ha ...

  3. windows(64位)下使用curl命令

    Curl命令可以通过命令行的方式,执行Http请求.在Elasticsearch中有使用的场景,因此这里研究下如何在windows下执行curl命令. 工具下载 在官网处下载工具包:http://cu ...

  4. jeecg中的一个上下文工具类获取request,session

    通过调用其中的方法可以获取到request和session,调用方式如下: HttpServletRequest request = ContextHolderUtils.getRequest();H ...

  5. linux(centos6)搭建ftp服务器 -摘自网络

    前提 ssh服务已经开启,关闭防火墙,主机和虚拟机能ping通 查看ssh和防火墙的状态 service sshd status service iptables status 开启ssh服务 ser ...

  6. 使用 NodeJS + Express 從 GET/POST Request 取值 -摘自网络

    過去無論哪一種網站應用程式的開發語言,初學者教學中第一次會提到的起手式,八九不離十就是 GET/POST Request 的取值.但是,在 Node.js + Express 的世界中,彷彿人人是高手 ...

  7. Linux学习笔记--SSH免password登录

    须要实现的效果: 有两台server:"192.168.201.236" 和 "192.168.201.237" 须要实现:在server"192.1 ...

  8. 精通D3.js笔记

    DOM常用属性 innerHTML: 元素标签内部的文本. innerText outerHTML outerText nodeName: 节点名称 parentNode: 父节点 nextSibli ...

  9. ubuntu 编译android 源码笔记

    已经验证,可以编译成功.过程中会碰到一些编译错误,安装好依赖环境,可以解决. 1.splite压缩包的合并,解压缩,md5验证 http://pan.baidu.com/s/1bnG1NtX kitk ...

  10. 常用的NodeJS模块

    图片处理 1.Manipulate images 官网:http://github.com/aheckmann/gm ImageMagick和GraphicsMagick主要用于图片的创建.编辑.合成 ...