【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样式是个很有意思,也很强大的,现在用的很多,很方便就能做出一个漂亮的报告,可以百度一下,语法相当简 ...
随机推荐
- 一个用C#实现的虚拟WiFi设置程序
前言: 本人常年使用Windows 7(虽然在努力学习Ubuntu,但是必须承认Windows 7上拥有大量的优秀软件,比如Evernote.Microsoft Office等).但是 ...
- 【转】iOS屏幕适配
一.iOS屏幕适配发展历程 设备 适配技术 4及以前(iPad未出) 直接用代码计算 有了iPad autoResizing 有不同屏幕的iPhone后 autoLayout 有更多不同屏幕的iPho ...
- 2014年9月21日_随笔,jdic,ETL,groovy,Nutz好多东西想学
(1)老妈十一要回老家,才突然发现买票好难啊.有亲朋很重要 (2)这周我做了什么.jdic,ETL,groovy, Nutz好多东西想学. Nutz开发成员专访.Nutz优酷视频(演讲).Nutz 入 ...
- 02_使用WebMagic爬虫获取CSDN推荐专家的个人博客信息
本来是想抓取博客园的博客推荐的页面的,但由于一些博客进去的页面格式都不太相同,一时不想花时间去寻找规律,发现CSDN上面的格式较为单一,就决定以CSDN推荐专家的个人博客信息作为爬虫抓取的目标. [首 ...
- Leetcode Count Prime
Description: Count the number of prime numbers less than a non-negative number, n Hint: The number n ...
- linux中python环境搭建及升级后yum不可用解决方案
1.1 LinuxCentOS 为例.1.1.1 升级 Python(1) 下载 Python 版本$ wget https://www.python.org/ftp/python/2.7.11/Py ...
- 【转】Nginx 服务器安装及配置文件详解
1. 安装nginx 1.1 选择稳定版本 我们编译安装nginx来定制自己的模块,机器CentOS 6.2 x86_64.首先安装缺少的依赖包: # yum -y install gcc gcc-c ...
- centos 不支持mysql
感觉是php-odbc必须装 yum -y install php php-fpm yum –y install php-mysql php-common php-mbstring php-gd ph ...
- z-index使用以及失效的处理方法
1.一般z-index的使用是在有两个重叠的标签,在一定的情况下控制其中一个在另一个的上方出现. 2.z-index值越大就越是在上层.z-index:9999:z-index元素的position属 ...
- linux安装ruby ruby-devel rubygems bundler
linux安装ruby ruby-devel rubygems yum install ruby ruby-devel rubygems 安装bundler gem install bundleror ...