使用Python读写xml文件

原文链接:http://wyb0.com/posts/python-read-and-write-xml/

0x00 解析XML的方法

  1. SAX (simple API for XML)
    python 标准库包含SAX解析器,SAX用事件驱动模型,通过在解析XML的过程中触发一个个的事件并调用用户定义的回调函数来处理XML文件。

  2. DOM(Document Object Model)
    将XML数据在内存中解析成一个树,通过对树的操作来操作XML。

  3. ElementTree(元素树)
    ElementTree就像一个轻量级的DOM,具有方便友好的API。代码可用性好,速度快,消耗内存少。

  • 我在这里使用ElementTree

0x01 Element对象的属性

每个Element对象都具有以下属性:

  • tag:string对象,表示数据代表的种类
  • attrib:dictionary对象,表示附有的属性
  • text:string对象,表示element的内容
  • tail:string对象,表示element闭合之后的尾迹
  • 若干子元素(child elements)
>>> from xml.etree import ElementTree as ET
>>> xml = """<books>
...   <book id='37476'>aaaa</book>
...   <book id='83727'>bbbb</book>
... </books>"""
>>> root = ET.fromstring(xml)
>>> root.tag
'books'
>>> child = root.getchildren()
>>> child
[<Element 'book' at 0x106f59410>, <Element 'book' at 0x106f59450>]
>>> child[0].tag
'book'
>>> child[0].attrib
{'id': '37476'}
>>> child[0].text
'aaaa'

0x02 文件内容

<?xml version='1.0' encoding='UTF-8'?>
<books>
  <book>
    <name>Python黑帽子</name>
    <date>2015</date>
    <price>37¥</price>
    <description>用python写一些程序</description>
  </book>
  <book>
    <name>Web安全深度剖析</name>
    <date>2014</date>
    <price>39¥</price>
    <description>讲述web渗透的基础知识</description>
  </book>
  <book>
    <name>白帽子讲web安全</name>
    <date>2013</date>
    <price>44¥</price>
    <description>道哥力作</description>
  </book>
</books>

0x03 读取xml节点

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from xml.etree import ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()
# root = ET.fromstring(country_data_as_string) #通过字符串导入,直接获取根
childs = root.getchildren()

books = []
for child0 in childs:
    book = {}
    for child00 in child0.getchildren():
        # print child00.tag #标签名,即name、date、price、description
        # print child00.text
        book[child00.tag] = child00.text
    books.append(book)

print books
"""
books = [
    {'name': 'Python黑帽子','date': '2015','price': '37¥','description': '用python写一些程序'},
    {'name': 'Web安全深度剖析','date': '2014','price': '39¥','description': '讲述web渗透的基础知识'},
    {'name': '白帽子讲web安全','date': '2013','price': '44¥','description': '道哥力作'}        
]
"""

0x04 写入xml文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from xml.etree.ElementTree import Element,ElementTree

books = [
    {
        'name': u'Python黑帽子',
        'date': '2015',
        'price': u'37¥',
        'description': u'用python写一些程序'
    },
    {
        'name': u'Web安全深度剖析',
        'date': '2014',
        'price': u'39¥',
        'description': u'讲述web渗透的基础知识'
    },
    {
        'name': u'白帽子讲web安全',
        'date': '2013',
        'price': u'44¥',
        'description': u'道哥力作'
    }        
]

def indent(elem, level=0):
    """美化写入文件的内容"""
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i


root = Element('books')
tree = ElementTree(root)

for book in books:
    child0 = Element('book')
    root.append(child0)

    for k,v in book.items():
        child00 = Element(k)
        child00.text = v
        child0.append(child00)

indent(root,0)
tree.write('aa.xml', 'UTF-8')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,898评论 18 399
  • 1 场景问题# 1.1 读取配置文件## 考虑这样一个实际的应用,维护系统自定义的配置文件。 几乎每个实际的应用系...
    七寸知架构阅读 8,307评论 2 56
  • 1. XML总结 1.1. XML简介 XML : 可扩展的标记语言。(和HTML非常类似的) 可扩展的。 自定义...
    Ethan_Walker阅读 8,349评论 0 12
  • 一. Java基础部分.................................................
    wy_sure阅读 9,233评论 0 11
  • 当你累了, 在飞过整片森林之后。 于是在风中小憩, 准备着后面无尽的海洋。 你无法停止飞翔, 就像你无法停止寻找归...
    安非他阅读 3,290评论 5 2