用python生成xml
2012-02-10 15:12
92 查看
2010-02-21 15:57
一个例子:
from xml.dom.minidom import Document
# Create the minidom document
doc = Document()
# Create the <wml> base element
wml = doc.createElement("wml")
doc.appendChild(wml)
# Create the main <card> element
maincard = doc.createElement("card")
maincard.setAttribute("id", "main")
wml.appendChild(maincard)
# Create a <p> element
paragraph1 = doc.createElement("p")
maincard.appendChild(paragraph1)
# Give the <p> elemenet some text
ptext = doc.createTextNode("This is a test!")
paragraph1.appendChild(ptext)
# Print our newly created XML
print doc.toprettyxml(indent=" ")
得到的xml文件:
<?xml version="1.0" ?>
<wml>
<card id="main">
<p>
This is a test!
</p>
</card>
</wml>
一个例子:
from xml.dom.minidom import Document
# Create the minidom document
doc = Document()
# Create the <wml> base element
wml = doc.createElement("wml")
doc.appendChild(wml)
# Create the main <card> element
maincard = doc.createElement("card")
maincard.setAttribute("id", "main")
wml.appendChild(maincard)
# Create a <p> element
paragraph1 = doc.createElement("p")
maincard.appendChild(paragraph1)
# Give the <p> elemenet some text
ptext = doc.createTextNode("This is a test!")
paragraph1.appendChild(ptext)
# Print our newly created XML
print doc.toprettyxml(indent=" ")
得到的xml文件:
<?xml version="1.0" ?>
<wml>
<card id="main">
<p>
This is a test!
</p>
</card>
</wml>
相关文章推荐
- python通过dom生成XML文档
- python 生成xml文件
- python菜鸟升级路--自动化解析生成xml文件
- python将xml xsl文件生成html文件存储示例讲解
- python之自动生成解析xml的C++类
- 【Python Network】使用DOM生成XML
- Python:Dom生成XML文件(写XML)
- Python基础(六) python生成xml测试报告
- Python生成PASCAL VOC格式的xml标注文件
- 使用Python生成XML的方法实例
- python 基于XML生成静态网页的简单实现
- python网络编程学习笔记:XML生成与解析(DOM、ElementTree)
- python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)
- python 生成xml
- 使用Python生成XML
- python xml+xsl 生成html文件
- python学习笔记(生成xml)
- Python:Dom生成XML文件(写XML)
- 【Python】 xml解析与生成 xml
- 使用 Python ElementTree 生成 xml