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. 【SQL】SQL中Case When的用法

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...

  2. MySQL Cluster

    MySQL Cluster MySQL集群一个非共享(shared nothing).分布式.分区系统,使用同步复制机制提供高可用和高性能. MySQL集群使用的是NDB引擎.NDB存储引擎会在节点间 ...

  3. python的内置下载器

    python有个内置下载器,有时候在内部提供文件下载很好用. 进入提供下载的目录 # ls abc.aaa chpw.py finance.py lsdir.py ping.py u2d-partia ...

  4. SAML

    From the book <Modern Authentication with Azure Active Directory for Web Applications> SAML Th ...

  5. Maven for Eclipse 第一章 ——Maven的介绍

    最近深陷与一个无比垃圾的项目无法自拔,好久没有更新文章了.今天简单介绍一下 Maven 在 Eclipse 中的使用.文章的内容几乎出于<Maven for Eclipse>一书,此书言简 ...

  6. 解决sklearn 随机森林数据不平衡的方法

    Handle Imbalanced Classes In Random Forest   Preliminaries # Load libraries from sklearn.ensemble im ...

  7. python进程间通信 实例

    python实现进程间通信简单实例 实例讲解了python实现两个程序之间通信的方法,具体方法:该实例采用socket实现,与socket网络编程不一样的是socket.socket(socket.A ...

  8. Android 相关的资源

    源码分析: http://blog.csdn.net/luoshengyang/article/details/8923485 中文博客: 英文博客: https://github.com/andro ...

  9. 最新Windows下c++读写锁SRWLock介绍

    https://blog.csdn.net/MoreWindows/article/details/7650574 https://blog.csdn.net/chenzhjlf/article/de ...

  10. 用casperjs模拟登录,支持多个账户登录

    var casper = require('casper').create({ viewportSize:{ width:1920, height:1080 } }); var url1 = 'htt ...