python写xml文件的操作实例
作者:shichen2014 发布时间:2023-08-09 00:40:39
标签:python,xml
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- 导语:Python如何下载网页上的图片呢?今天小编给大家分享另一个Python应用小程序,就是:用Python控制摄像头录制视频!学会了也可
- isnull在数据库查询中的应用,特别是再语句连接的时候需要用到 比如连接时候,某个字段没有值但是又要左连接到其他表上 就会显示空, isn
- 本文实例讲述了Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能。分享给大家供大家参考,具体如下:因为需要使用
- 测试代码1:def test(self): data = {"add": {"doc":
- 认为整理的还比较详细的,亲们,就快点收藏起来吧!PHP系统类函数assert函数:检查assertion声明是否错误extension_lo
- range()函数配合循环一块使用很方便,今天来整理一下这个函数的主要用法,以及几个小例子首先来看一下函数原型,非常简单: range(st
- 过程名:ManualPagination作 用:采用手动分页方式显示文章具体的内容参 数:ArticleID,strContentSub&n
- 01. 装饰器语法糖如果你接触 Python 有一段时间了的话,想必你对 @ 符号一定不陌生了,没错 @ 符号就是装饰器的语法糖。它放在一个
- python爬虫模块selenium简介selenium主要是用来做自动化测试,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问
- 1 关于 Matplotlib 模块Matplotlib 是一个由 John Hunter 等开发的,用以绘制二维图形的 Python 模块
- 操作步骤导入框架,import unitest测试类必须继承类:.class 类名(unittest.TestCase):在类中所有定义te
- 关于mysql数据库在Linux下的应用一直以来都是我认为比较棘手的,这次通过搭建Linux学习环境顺便研究和学习Mysql数据库在Linu
- 本文实例讲述了Python根据指定日期计算后n天,前n天是哪一天的方法。分享给大家供大家参考,具体如下:# -*- coding:utf-8
- 当然首先得去下载ASPupload 程序,安装后使用!官方网站下载:http://www.aspupload.com/使用ASP实现文件上载
- 一、开发环境集成开发工具:jupyter notebook 6.5.2集成开发环境:Python 3.10.6第三方库:tensorflow
- 一、安装selenium打开命令控制符输入:pip install -U selenium火狐浏览器安装firebug:www.firebu
- 实例一--爬取页面import requestsurl="https//itemjd.com/2646846.html"
- 概 述 现在有不少介绍利用ASP实现动态分页的文章,方法大同小异,就是每次利用ADO返回原始数据满足条件记录集中的指定
- 这个函数是前几年刚流行小偷程序的时候,偶写来用于小偷程序中截取代码的;可能有些朋友在我以前的代码中看见过了,但没有写用法,现在把调用方法及使
- 1. 引言作为一名算法工程师,如何快速实现一个想法并验证它是否有效对日常工作至关重要。Python 是一个出色的工具,可以很方便地实现这一点