python写xml文件的操作实例

作者:shichen2014 时间:2023-08-09 00:40:39 

本文实例讲述了python写xml文件的操作的方法,分享给大家供大家参考。具体方法如下:

要生成的xml文件格式如下:


<?xml version="1.0" ?>
<!--Simple xml document__chapter 8-->
<book>
 <title>
   sample xml thing
 </title>
 <author>
   <name>
     <first>
       ma
     </first>
     <last>
       xiaoju
     </last>
   </name>
   <affiliation>
     Springs Widgets, Inc.
   </affiliation>
 </author>
 <chapter number="1">
   <title>
     First
   </title>
   <para>
     I think widgets are greate.You should buy lots of them forom
     <company>
       Spirngy Widgts, Inc
     </company>
   </para>
 </chapter>
</book>

Python实现代码如下:


from xml.dom import minidom, Node

doc = minidom.Document()

doc.appendChild(doc.createComment("Simple xml document__chapter 8"))

#generate the book
book = doc.createElement('book')
doc.appendChild(book)

#the title
title = doc.createElement('title')
title.appendChild(doc.createTextNode("sample xml thing"))
book.appendChild(title)

#the author section
author = doc.createElement("author")
book.appendChild(author)
name = doc.createElement('name')
author.appendChild(name)
firstname = doc.createElement('first')
firstname.appendChild(doc.createTextNode("ma"))
name.appendChild(firstname)
lastname = doc.createElement('last')
name.appendChild(lastname)
lastname.appendChild(doc.createTextNode("xiaoju"))

affiliation = doc.createElement("affiliation")
affiliation.appendChild(doc.createTextNode("Springs Widgets, Inc."))
author.appendChild(affiliation)

#The chapter
chapter = doc.createElement('chapter')
chapter.setAttribute('number', '1')
title = doc.createElement('title')
title.appendChild(doc.createTextNode("First"))
chapter.appendChild(title)
book.appendChild(chapter)

para = doc.createElement('para')
para.appendChild(doc.createTextNode("I think widgets are greate.\
You should buy lots of them forom"))
company = doc.createElement('company')
company.appendChild(doc.createTextNode("Spirngy Widgts, Inc"))
para.appendChild(company)
chapter.appendChild(para)

print doc.toprettyxml()

希望本文所述对大家的Python程序设计有所帮助。

标签:python,xml
0
投稿

猜你喜欢

  • PHP图像识别技术原理与实现

    2024-06-05 09:43:54
  • 在Python中使用异步Socket编程性能测试

    2023-04-20 23:19:56
  • pyecharts动态轨迹图的实现示例

    2021-04-23 08:14:58
  • Flask 入门系列 Cookie与session的介绍

    2022-06-21 00:45:44
  • OpenCV 表盘指针自动读数的示例代码

    2023-12-29 05:46:38
  • 一个js自动完成功能源码

    2011-06-06 07:42:00
  • python实现的多线程端口扫描功能示例

    2023-02-02 10:18:29
  • 初瞥 Google Chrome Frame

    2009-10-06 14:41:00
  • PyQT实现菜单中的复制,全选和清空的功能的方法

    2023-08-13 03:09:23
  • mysql如何统计同一字段不同值的个数

    2024-01-26 17:46:33
  • 用python完成一个分布式事务TCC

    2022-12-02 02:14:05
  • IDEA开启Run Dashboard的配置详解

    2023-03-16 10:34:51
  • Seaborn数据分析NBA球员信息数据集

    2021-06-27 03:36:04
  • 深入浅析python 协程与go协程的区别

    2022-02-16 23:57:26
  • JS图片懒加载的优点及实现原理

    2024-04-18 09:45:34
  • 解决Python图形界面中设置尺寸的问题

    2022-11-12 12:05:02
  • Python 马氏距离求取函数详解

    2023-08-27 01:28:58
  • Python入门教程之运算符重载详解

    2021-10-12 20:15:28
  • vue props 一次传多个值实例

    2024-05-03 15:10:32
  • Java连接各种数据库的方法

    2024-01-28 10:56:26
  • asp之家 网络编程 m.aspxhome.com