【Python Network】使用DOM生成XML
单纯的为DOM树添加结点。
#!/usr/bin/env python
# Generating XML with DOM - Chapter 8 - domgensample.py from xml.dom import minidom, Node doc = minidom.Document() doc.appendChild(doc.createComment("Sample XML Document - Chapter 8 -")) # Generate 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')
name.appendChild(firstname)
firstname.appendChild(doc.createTextNode('Benjamin'))
name.appendChild(doc.createTextNode(' '))
lastname = doc.createElement('last')
name.appendChild(lastname)
lastname.appendChild(doc.createTextNode('Smith')) affiliation = doc.createElement('affiliation')
author.appendChild(affiliation)
affiliation.appendChild(doc.createTextNode('Spring Widgets, Inc.')) # The chapter
chapter = doc.createElement('chapter')
book.appendChild(chapter)
chapter.setAttribute('number', '')
title = doc.createElement('title')
chapter.appendChild(title)
title.appendChild(doc.createTextNode('First Chapter')) para = doc.createElement('para')
chapter.appendChild(para)
para.appendChild(doc.createTextNode("I think widgets are great. You" + " should buy lots of them from "))
company = doc.createElement('company')
para.appendChild(company)
company.appendChild(doc.createTextNode('Spring Widgets, Inc')) para.appendChild(doc.createTextNode('.')) print doc.toprettyxml(indent = ' ')
【Python Network】使用DOM生成XML的更多相关文章
- Python:Dom生成XML文件(写XML)
http://www.ourunix.org/post/327.html 在python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文 ...
- DOM生成XML文档与解析XML文档(JUNIT测试)
package cn.liuning.test; import java.io.File; import java.io.IOException; import javax.xml.parsers.D ...
- python学习笔记(生成xml)
想着给框架加些功能 首先想到的是生成测试报告 这里就涉及到了生成什么格式的文件 我这边就准备生成 xml 格式的文件 自己先学习了整理了下 代码如下: #!/usr/bin/env python # ...
- Dom生成Xml和解析Xml
xml这样的文件格式在非常多时候都是非常适合我们用来存取数据的,所以利用程序来生成xml文件和解析xml文件就显得比較重要了.在dom中是把每个元素都看做是一个节点Node的,全部页面上的属性.元素等 ...
- DOM生成XML文件
/** * 从数据库读取学生信息的数据集合,然后Dom创建数据树,再转成XML格式数据,输出生成xml文件 * @author pikaqiu * */ public class TestGenXml ...
- DOM生成XML文档
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuil ...
- Python—使用xm.dom解析xml文件
什么是DOM? 文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展置标语言的标准编程接口. 一个 DOM 的解析器在解析一个 XML 文档时,一次性读 ...
- Python中使用dom模块生成XML文件示例
在Python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文件,下一篇文章再继续介绍Dom解析XML文件. 在生成XML文件中,我们主 ...
- Python基础(六) python生成xml测试报告
思路: 1.使用xslt样式,这样可以很好的和xml结合,做出漂亮的报告 2.生成xml结构 xslt样式是个很有意思,也很强大的,现在用的很多,很方便就能做出一个漂亮的报告,可以百度一下,语法相当简 ...
随机推荐
- poj1308 Is It A Tree?(并查集)详解
poj1308 http://poj.org/problem?id=1308 题目大意:输入若干组测试数据,输入 (-1 -1) 时输入结束.每组测试数据以输入(0 0)为结束标志.然后根据所给的 ...
- 使用ROW_NUMBER进行的快速分页
DECLARE @pageSize INT ; DECLARE @pageIndex INT ; SET @pageSize = 5 SET @pageIndex =2 ; --第二页,每页显示5条数 ...
- ACM/ICPC ZOJ1003-Crashing Balloon 解题代码
#include <iostream> using namespace std; int main() { int **array = new int *[100]; for ( int ...
- android使用广播退出应用程序
由于在(Widget或Service.BroadcastReceiver中)使用startActivity()方法启动activity时需使用FLAG_ACTIVITY_NEW_TASK flag,所 ...
- MVC 读取图片
/// <summary> /// 获取上传的图片 /// </summary> public FileResult GetImages(string PhotoPath) ...
- c#打开txt文件并导入到textbox中
OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = " 请选择您要导入的模板文件:&qu ...
- SDL实现限制帧速
很多人都在SDL_PollEvent和SDL_WaitEvent之间纠结.前者会带来更好的帧数表现,但是CPU占用极大,可以直接吃掉一个核心.后者则基本不占用CPU,但是帧数会受到影响.有没有办法使两 ...
- javascript 布局 第20节
<html> <head> <title>页面布局</title> <style type="text/css"> bo ...
- linux管理文件系统指令
就一个基本的linux系统而言,其计算机硬盘只能有三个分区:一个交换分区(用于处理物理内存存不下的信息),一个包含引导转载程序的内核的启动分区,一个根文件系统分区,后两个常采用 ext3文件系统 与e ...
- 第3章文件I/O总结
1. open和create函数在fcntl.h中,close.lseek.read.write函数在unistd.h中 open函数通过进程有效用户ID判断读文件的权限 可以调用access函数判断 ...