易截截图软件、单文件、免安装、纯绿色、仅160KB

python 中minidom解析xml

python 中minidom解析xml
2009年06月26日 星期五 08:40
下面只列出一些常用的方法属性,如果要查看更多的方法, 可以去看文件minidom如何实现的。
获得Document对象
法一:
import xml.dom.minidom as m_dom
doc1 = m_dom.getDOMImplementation().createDocument(None, "root1", None)
doc1.documentElement.toxml(encoding="utf-8")
'<root1/>'
法二:
s = "<node><xxxx/></node>"
doc2 = m_dom.parseString(s)
print doc2.toprettyxml(indent="    ", newl="\n", encoding="utf-8")
<?xml version="1.0" encoding="utf-8"?>
<node>
    <xxxx/>
</node>
获得跟节点:
rt = doc1.documentElement
创建节点:
node1 = doc1.createElement("node1")
rt.appendChild(node1)
rt.toxml()
'<root1><node1/></root1>'
设置/获得属性:
node1.setAttribute("name", "name1")
rt.toxml()
'<root1><node1 name="name1"/></root1>'
node1.getAttribute("name")
'name1'
创建数据节点:
dt = doc1.createTextNode("the datas.")
node1.appendChild(dt)
rt.toxml()
'<root1><node1 name="name1">the datas.</node1></root1>'
node1.firstChild.toxml()
'the datas.'
获得子节点:
node1
<DOM Element: node1 at 0xed0aa8>
rt.childNodes
[<DOM Element: node1 at 0xed0aa8>]
rt.getElementsByTagName("node1")
[<DOM Element: node1 at 0xed0aa8>]
rt.firstChild
<DOM Element: node1 at 0xed0aa8>
node2 = doc1.createElement("node2")
rt.appendChild(node2)
<DOM Element: node2 at 0xe890a8>
rt.toxml()
'<root1><node1 name="name1">the datas.</node1><node2/></root1>'
rt.childNodes
[<DOM Element: node1 at 0xed0aa8>, <DOM Element: node2 at 0xe890a8>]
node1.firstChild
<DOM Text node "the datas.">
删除子节点:
n2 = rt.childNodes[1]
rt.removeChild(n2)
<DOM Element: node2 at 0xe890a8>
rt.toxml()
'<root1><node1 name="name1">the datas.</node1></root1>'
node1.removeChild(node1.firstChild)
<DOM Text


相关文档:

用C语言扩展Python的功能


原文
Python和C分别有着各自的优缺点,用Python开发程序速度快,可靠性高,并且有许多现成模块可供使用,但执行速度相对较慢;C语言则正好相反,其执行速度快,但开发效率低。为了充分利用两种语言各自的优点,比较好的做法是用Python开发整个软件框架,而用C语言实现其关键模块。本文介绍如何利用C语言来扩展Python的功 ......

python应用领域介绍

 
Python作为一种功能强大且通用的编程语言而广受好评,它具有非常清晰的语法特点,适用于多种操作系统,目前在国际上非常流行,正在得到越来越多的应用。 
下面就让我们一起来看看它的强大功能: 
Python(派森),它是一个简单的、解释型的、交互式的、可移植的、面向对象的超高级语言。这就是对Python ......

Python 和 bash shell脚本

作为一个初学shell的程序员来说,使用shell的一些命令调用,比如就写几行,做一些自动化的操作,简单又实用,但是涉及逻辑控制和
字符串处理时,看到那if/fi,case/esac……还有古离古怪的布尔判断……还有复杂而又名字很响而且很老资格的sed、awk……我就想,
在没有非要在shell中 ......

python猜数字游戏

from random import randint              #导入随即函数
def guessNum():                          &nbs ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号