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

[python]

sample xml thing

ma

xiaoju

Springs Widgets, Inc.

First

I think widgets are greate.You should buy lots of them forom

Spirngy Widgts, Inc

代码:

[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中的minidom写过生成XML文件的程序,现在需要读取XML文件中的内容了,首先想到的还是minidom模块.一番编写测试后,如愿掌握了其函数的使用方式,和AJAX中的DOM操作没什么区别.

以前就知道elementtree在处理XML文件时广受Python程序员的欢迎,也安装过elementtree的安装包,现在使用的Python2.5中已将其收录了.既然我要处理XML文件,当然也要学着使用更高效和易用的模块了.自己摸索了半天,除了有关名字空间的函数没有试用外,其它函数都试用过了.以后处理XML文件可以得心应手了。

下面是一个简单的例子,通过它可以知道各个函数的使用方法:

from xml.etree.ElementTree import ElementTree

from xml.etree.ElementTree import Element

from xml.etree.ElementTree import SubElement

from xml.etree.ElementTree import dump

from xml.etree.ElementTree import Comment

from xml.etree.ElementTree import tostring

'''

Potato Smasher

Smash Potatoes like never before.

'''

## Writing the content to xml document

book = ElementTree()

purchaseorder = Element('PurchaseOrder')

book._setroot(purchaseorder)

SubElement(purchaseorder, 'account', {'refnum' : "2390094"})

item = Element("item", {'sku' : '33-993933', 'qty' : '4'})

purchaseorder.append(item)

print item.items() # [('sku', '33-993933'), ('qty', '4')]

print item.attrib # {'sku': '33-993933', 'qty': '4'}

print item.get('sku') # 33-993933

SubElement(item, 'name').text = "Potato Smasher"

SubElement(item, 'description').text = "Smash Potatoes like never before."

#book.write('book.xml',"utf-8")

#print tostring(purchaseorder)

#import sys

#book.write(sys.stdout)

#dump(book)

## Displaying the content of the xml document

print purchaseorder.find('account')

print purchaseorder.find('account').get('refnum')

print purchaseorder.findall('account')[0].get('refnum')

print purchaseorder.find('item/name')

print purchaseorder.find('item/name').text

## How to use ElementTree([element,] [file])

## 1. From standard XML element, it becomes root element

print ElementTree(item).getroot().find('name').text

## 2. From XML file

print ElementTree(file='book.xml').getroot().find('item/description').text

## Create an iterator

for element in purchaseorder.getiterator():

print element.tag

## Get pretty look

def indent(elem, level=0):

i = "\n" + level*""

if len(elem):

if not elem.text or not elem.text.strip():

elem.text = i + ""

for e in elem:

indent(e, level+1)

if not e.tail or not e.tail.strip():

e.tail = i

if level and (not elem.tail or not elem.tail.strip()):

elem.tail = i

return elem

if __name__=="__main__":

dump(indent(purchaseorder))

book.write('book.xml',"utf-8")

python读xml文件生成.h头文件_PYTHON读写xml文件的方法相关推荐

  1. gsoap 学习 1-由wsdl文件生成h头文件

    开始前先看一下用户向导吧 http://www.cs.fsu.edu/~engelen/soap.html 中左侧点击Documentation 英语水平确实有限,有些内容可能说的不准确,敬请参考向导 ...

  2. python读xml文件生成.h头文件_Python创建xml文件示例

    Python创建xml文件示例 这里有新鲜出炉的 Python 入门,程序狗速度看过来! Python 编程语言 Python 是一种面向对象.解释型计算机程序设计语言,由 Guido van Ros ...

  3. python本地读csv文件_python读写csv文件方法详细总结

    python提供了大量的库,可以非常方便的进行各种操作,现在把python中实现读写csv文件的方法使用程序的方式呈现出来. 在编写python程序的时候需要csv模块或者pandas模块,其中csv ...

  4. hpp头文件和h头文件

    详解:C++ hpp头文件与h头文件-C++ hpp文件-嗨客网 个人见解: hpp只是为了让人知道这个文件声明和定义都在同一个文件,仅此而已.至于上面文章说的增加cpp和编译次数 1.完全可以用.h ...

  5. python不同数据的读入_python读写不同编码txt文件_python读写txt文件

    python读写不同编码txt文件_python读写txt文件 以后整理规范 [python] view plaincopy import os import codecs filenames=os. ...

  6. Android JNI开发生成.h头文件问题(转)

    在JNI开发中,首先要将建立的anroid类编译成.h文件,编译用到命令javah,由于第一次用,以前对java的编译过程也不怎么了解,所以走了好多弯路,网络没有对这一步的详细介绍,这里讲一下: 通过 ...

  7. java生成.h头文件_javah头文件生成器

    javah头文件生成器 javah程序创建C头文件和存根文件,这些是把本地C成员函数包入java 所需要的.被创建的头文件给出了有关java类的信息,这些信息是C成员函数与java类交换数据所必需的. ...

  8. hpp头文件与h头文件的区别

    hpp,其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的调用者只需要include该hpp文件即可,无需再将cpp加入到project中进行编译.而实现代码将直接 ...

  9. python读docx文件_Python读写docx文件的方法

    Python读写word文档有现成的库可以处理.我这里采用 python-docx.可以用pip install python-docx安装一下. 这里说一句,ppt和excel也有类似的库哦,而且是 ...

最新文章

  1. Javascript typeof用法
  2. 2021东营市地区高考成绩排名查询,东营高中学校实力排名,2021年东营所有的高中分数线排名...
  3. php网页打开输入密码,PHP输入密码查看网页
  4. 获取IP地址和MAC地址
  5. 天梯 L2 这是二叉搜索树吗?
  6. 高阶函数 - 函数节流
  7. 解决 -- java 调用webservice 服务端收到参数为null
  8. Lightroom 如何安装lrplugin格式插件
  9. 『ExtJS』表单(一)常用表单控件及内置验证
  10. 关于iOS中MVC和MVVM的区别和联系
  11. M2Det 论文解读
  12. 利用Depends查看win系统下exe程序的依赖项
  13. SQL Server-【知识与实战III】年龄查询、条件查询、多表查询、姓氏查询
  14. Vant_根据已有地址获取code
  15. java 分层处理解耦_后端分层架构如何解耦?
  16. Kafka---如何配置Kafka集群和zookeeper集群
  17. [unity]使用Unity粒子系统制作跑尘
  18. Webpack实用工具之webpack-server
  19. vue脚手架实现留言板功能
  20. 数字图像处理 使用opencv+python识别七段数码显示器的数字

热门文章

  1. 道通智能开始招聘啦,待遇超好哦!!!
  2. 拾起童年的回忆 - 手把手教你制作飞机大战小游戏
  3. 【大熊智能nrf connect使用方法
  4. 单例模式(饿汉单例模式and懒汉单例模式)
  5. 毕业设计-基于spring boot的智慧物业管理系统
  6. java 部署策略_如何使用TeamCity和Octopus完成此分支和部署策略
  7. Jasperreport_6.18的回血终结篇之案例目录、封面
  8. 做百度快照入门_如何为您的博客文章拍摄屏幕快照(入门指南)
  9. 究极方法!!!CSDN图片上传失败无法重新上传和删除的解决
  10. polished css,CSS in JS