设为首页 加入收藏

TOP

使用Python创建xml文件
2014-11-24 12:54:04 来源: 作者: 【 】 浏览:0
Tags:使用 Python 创建 xml 文件

使用Python创建xml文件:


from xml.dom.minidomimport Document



# Create the minidom document
doc = Document()



# Create the base element
wml = doc.createElement("wml")doc.appendChild(wml)



# Create the main element
maincard = doc.createElement("card")maincard.setAttribute("id","main")wml.appendChild(maincard)



# Create a

element
paragraph1 = doc.createElement("p")maincard.appendChild(paragraph1)



# Give the

elemenet some text
ptext = doc.createTextNode("This is a test!")paragraph1.appendChild(ptext)

# Print our newly created XML to console
print doc.toprettyxml(indent=" ")


# Print our newly created XML to d:\out.xml
with open("d:\out.xml", "w") as f:
f.write(doc.toprettyxml(indent=" ")


输出:
< xml version="1.0" >



This is a test!




】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Real6410移植Boa Webserver详解 下一篇iOS视图控制器与标签传递nib的输..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)