Python3 XML 解析

python3 xml 解析

 

xml 指可扩展标记语言(extensible markup language),是一种用于标记电子文件使其具有结构性的标记语言。xml 通常被用来设计传输和存储数据。

python 支持对 xml 的解析,常见的 xml 编程接口有 dom 和 sax,这两种接口处理 xml 文件的方式不同,当然使用场合也不同。

1.sax (simple api for xml )

python 标准库包含 sax 解析器,sax 用事件驱动模型,通过在解析 xml 的过程中触发一个个的事件并调用用户定义的回调函数来处理 xml 文件。

2.dom(document object model)

将 xml 数据在内存中解析成一个树,通过对树的操作来操作 xml。

本章节使用到的 xml 范例文件 movies.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> 

 

python 使用 sax 解析 xml

sax 是一种基于事件驱动的api。

利用 sax 解析 xml 文档牵涉到两个部分: 解析器和事件处理器。

解析器负责读取 xml 文档,并向事件处理器发送事件,如元素开始跟元素结束事件。

而事件处理器则负责对事件作出响应,对传递的 xml 数据进行处理。

  • 1、对大型文件进行处理;
  • 2、只需要文件的部分内容,或者只需从文件中得到特定信息。
  • 3、想建立自己的对象模型的时候。

在 python 中使用 sax 方式处理 xml 要先引入 xml.sax 中的 parse 函数,还有 xml.sax.handler 中的 contenthandler。

contenthandler 类方法介绍

characters(content) 方法

调用时机:

从行开始,遇到标签之前,存在字符,content 的值为这些字符串。

从一个标签,遇到下一个标签之前, 存在字符,content 的值为这些字符串。

从一个标签,遇到行结束符之前,存在字符,content 的值为这些字符串。

标签可以是开始标签,也可以是结束标签。

startdocument() 方法

文档启动的时候调用。

enddocument() 方法

解析器到达文档结尾时调用。

startelement(name, attrs) 方法

遇到xml开始标签时调用,name 是标签的名字,attrs 是标签的属性值字典。

endelement(name) 方法

遇到xml结束标签时调用。

 

make_parser 方法

以下方法创建一个新的解析器对象并返回。

xml.sax.make_parser( [parser_list] )

参数说明:

  • parser_list - 可选参数,解析器列表

 

parser 方法

以下方法创建一个 sax 解析器并解析xml文档:

xml.sax.parse( xmlfile, contenthandler[, errorhandler])

参数说明:

  • xmlfile - xml文件名
  • contenthandler - 必须是一个 contenthandler 的对象
  • errorhandler - 如果指定该参数,errorhandler 必须是一个 sax errorhandler 对象

 

<3>parsestring 方法

parsestring 方法创建一个 xml 解析器并解析 xml 字符串:

xml.sax.parsestring(xmlstring, contenthandler[, errorhandler])

参数说明:

  • xmlstring - xml字符串
  • contenthandler - 必须是一个 contenthandler 的对象
  • errorhandler - 如果指定该参数,errorhandler 必须是一个 sax errorhandler对象

 

python 解析xml范例

#!/usr/bin/python3

import xml.sax

class moviehandler( xml.sax.contenthandler ):
   def __init__(self):
      self.currentdata = ""
      self.type = ""
      self.format = ""
      self.year = ""
      self.rating = ""
      self.stars = ""
      self.description = ""

   # 元素开始调用
   def startelement(self, tag, attributes):
      self.currentdata = tag
      if tag == "movie":
         print ("*****movie*****")
         title = attributes["title"]
         print ("title:", title)

   # 元素结束调用
   def endelement(self, tag):
      if self.currentdata == "type":
         print ("type:", self.type)
      elif self.currentdata == "format":
         print ("format:", self.format)
      elif self.currentdata == "year":
         print ("year:", self.year)
      elif self.currentdata == "rating":
         print ("rating:", self.rating)
      elif self.currentdata == "stars":
         print ("stars:", self.stars)
      elif self.currentdata == "description":
         print ("description:", self.description)
      self.currentdata = ""

   # 读取字符时调用
   def characters(self, content):
      if self.currentdata == "type":
         self.type = content
      elif self.currentdata == "format":
         self.format = content
      elif self.currentdata == "year":
         self.year = content
      elif self.currentdata == "rating":
         self.rating = content
      elif self.currentdata == "stars":
         self.stars = content
      elif self.currentdata == "description":
         self.description = content
  
if ( __name__ == "__main__"):
   
   # 创建一个 xmlreader
   parser = xml.sax.make_parser()
   # 关闭命名空间
   parser.setfeature(xml.sax.handler.feature_namespaces, 0)

   # 重写 contexthandler
   handler = moviehandler()
   parser.setcontenthandler( handler )
   
   parser.parse("movies.xml")

以上代码执行结果如下:

*****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
rating: pg
stars: 10
description: vash the stampede!
*****movie*****
title: ishtar
type: comedy
format: vhs
rating: pg
stars: 2
description: viewable boredom

 

使用xml.dom解析xml

文件对象模型(document object model,简称dom),是w3c组织推荐的处理可扩展置标语言的标准编程接口。

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

python中用xml.dom.minidom来解析xml文件,范例如下:

#!/usr/bin/python3

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"))

   type = movie.getelementsbytagname('type')[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)

以上程序执行结果如下:

root element : new arrivals
*****movie*****
title: enemy behind
type: war, thriller
format: dvd
rating: pg
description: talk about a us-japan war
*****movie*****
title: transformers
type: anime, science fiction
format: dvd
rating: r
description: a schientific fiction
*****movie*****
title: trigun
type: anime, action
format: dvd
rating: pg
description: vash the stampede!
*****movie*****
title: ishtar
type: comedy
format: vhs
rating: pg
description: viewable boredom

下一节:python3 json 解析

python3 教程

相关文章