python的xml.dom学习笔记

简介:

首先说一下,由于这篇文章主要是自己随性学习写的,所以读者看起来可能很乱,呵呵。可以给大家稍微推荐一篇:http://www.cnblogs.com/xuxm2007/archive/2011/01/16/1936610.html 稍微清晰一点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#coding=utf-8
 
#解析xml文件中的所有的link标签
from  xml.dom  import  minidom
from  xml.dom.minidom  import  getDOMImplementation
doc = minidom.parse( "d:\\hello.html" )
 
nodes = doc.getElementsByTagName( "link" )
 
for  node  in  nodes:
     print  "<" ,node.tagName,
     print  "type=\"" ,node.getAttribute( "type" ), "\"" ,
     print  "rel=\"" ,node.getAttribute( "rel" ), "\"" ,
     print  "href=\"" ,node.getAttribute( "href" ), "\"" ,
     print  "/>"
     
print  "通过另外一种方式获得link标签"
linknodes = doc.getElementsByTagName( "link" )
for  i  in  range ( len (linknodes)):
     print  linknodes[i].getAttribute( "type" ),
     print  linknodes[i].getAttribute( "rel" ),
     print  linknodes[i].getAttribute( "href" )
     
 
#操作节点
node = linknodes[ 0 ]
print  dir (node)
print  node.parentNode
print  node.prefix
print  node.nodeType,node.nodeValue,node.nodeName
print  node.localName
print  node.childNodes
print  node.firstChild,node.lastChild
print  node.attributes
print  node.namespaceURI
print  node.nextSibling
print  "--" * 10
print  node.tagName
 
print  "===" * 20
impl = getDOMImplementation()
newdoc = impl.createDocument( None  ,  "some_tag" ,  None )
top_element = newdoc.documentElement
node1 = newdoc.createTextNode( "node1" )
node2 = newdoc.createTextNode( "node2" )
node3 = newdoc.createTextNode( "node3" )
 
top_element.appendChild(node1)
top_element.appendChild(node2)
top_element.appendChild(node3)
 
top_element.removeChild(node3)
top_element.insertBefore(node3,node2)
 
print  top_element.childNodes

  运行结果:

< link type = " text/css "  rel = " stylesheet "  href = " http://www.cnblogs.com/css/common.css "  / >
< link type = " text/css "  rel = " stylesheet "  href = " http://www.cnblogs.com/Skins/kubrick/style.css "  / >
< link type = " text/css "  rel = " stylesheet "  href = " http://www.cnblogs.com/css/common2.css "  / >
< link type = " text/css "  rel = " stylesheet "  href = " http://common.cnblogs.com/css/shCore.css "  / >
< link type = " text/css "  rel = " stylesheet "  href = " http://common.cnblogs.com/css/shThemeDefault.css "  / >
< link type = " application/rss+xml "  rel = " alternate "  href = " http://www.cnblogs.com/rollenholt/rss "  / >
< link type = " application/rsd+xml "  rel = " EditURI "  href = " http://www.cnblogs.com/rollenholt/rsd.xml "  / >
< link type = " application/wlwmanifest+xml "  rel = " wlwmanifest "  href = " http://www.cnblogs.com/rollenholt/wlwmanifest.xml "  / >
通过另外一种方式获得link标签
text / css stylesheet http: / / www.cnblogs.com / css / common.css
text / css stylesheet http: / / www.cnblogs.com / Skins / kubrick / style.css
text / css stylesheet http: / / www.cnblogs.com / css / common2.css
text / css stylesheet http: / / common.cnblogs.com / css / shCore.css
text / css stylesheet http: / / common.cnblogs.com / css / shThemeDefault.css
application / rss + xml alternate http: / / www.cnblogs.com / rollenholt / rss
application / rsd + xml EditURI http: / / www.cnblogs.com / rollenholt / rsd.xml
application / wlwmanifest + xml wlwmanifest http: / / www.cnblogs.com / rollenholt / wlwmanifest.xml
[ 'ATTRIBUTE_NODE' , 'CDATA_SECTION_NODE' , 'COMMENT_NODE' , 'DOCUMENT_FRAGMENT_NODE' , 'DOCUMENT_NODE' , 'DOCUMENT_TYPE_NODE' , 'ELEMENT_NODE' , 'ENTITY_NODE' , 'ENTITY_REFERENCE_NODE' , 'NOTATION_NODE' , 'PROCESSING_INSTRUCTION_NODE' , 'TEXT_NODE' , '__doc__' , '__init__' , '__module__' , '__nonzero__' , '__repr__' , '_attrs' , '_attrsNS' , '_call_user_data_handler' , '_child_node_types' , '_get_attributes' , '_get_childNodes' , '_get_firstChild' , '_get_lastChild' , '_get_localName' , '_get_tagName' , '_magic_id_nodes' , 'appendChild' , 'attributes' , 'childNodes' , 'cloneNode' , 'firstChild' , 'getAttribute' , 'getAttributeNS' , 'getAttributeNode' , 'getAttributeNodeNS' , 'getElementsByTagName' , 'getElementsByTagNameNS' , 'getInterface' , 'getUserData' , 'hasAttribute' , 'hasAttributeNS' , 'hasAttributes' , 'hasChildNodes' , 'insertBefore' , 'isSameNode' , 'isSupported' , 'lastChild' , 'localName' , 'namespaceURI' , 'nextSibling' , 'nodeName' , 'nodeType' , 'nodeValue' , 'normalize' , 'ownerDocument' , 'parentNode' , 'prefix' , 'previousSibling' , 'removeAttribute' , 'removeAttributeNS' , 'removeAttributeNode' , 'removeAttributeNodeNS' , 'removeChild' , 'replaceChild' , 'schemaType' , 'setAttribute' , 'setAttributeNS' , 'setAttributeNode' , 'setAttributeNodeNS' , 'setIdAttribute' , 'setIdAttributeNS' , 'setIdAttributeNode' , 'setUserData' , 'tagName' , 'toprettyxml' , 'toxml' , 'unlink' , 'writexml' ]
<DOM Element: head at 0x1b3e968 >
None
1  None  link
link
[]
None  None
<xml.dom.minidom.NamedNodeMap object  at 0x01B4D648 >
http: / / www.w3.org / 1999 / xhtml
<DOM Text node "u'\n'" >
- - - - - - - - - - - - - - - - - - - -
link
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
[<DOM Text node "'node1'" >, <DOM Text node "'node3'" >, <DOM Text node "'node2'" >]

  


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2011/12/01/2271131.html,如需转载请自行联系原作者
相关文章
|
XML 存储 Java
Python-docx编号列表解析:从XML迷宫到结构化数据的破局之道
本文深入解析了Word文档中自动编号的存储机制及解析难题,探讨了其在技术处理中的障碍,并通过三种实战方案对比,帮助开发者高效提取结构化数据。内容涵盖底层XML结构、常见问题解决方案及性能优化技巧,适用于合同条款、文档自动化处理等场景。
1119 0
|
XML 存储 缓存
Python-docx编号列表解析:从XML迷宫到结构化数据的破局之道
Word文档中的自动编号功能虽便捷,却在技术处理时常成为障碍。本文深入解析Word文档的XML结构,揭示编号信息的存储机制,并提供多种基于Python的解决方案,涵盖跨平台解析、性能优化及固定模板处理,助力开发者突破编号解析难题,实现合同条款等内容的结构化提取与处理。
667 0
|
网络协议 Java Linux
PyAV学习笔记(一):PyAV简介、安装、基础操作、python获取RTSP(海康)的各种时间戳(rtp、dts、pts)
本文介绍了PyAV库,它是FFmpeg的Python绑定,提供了底层库的全部功能和控制。文章详细讲解了PyAV的安装过程,包括在Windows、Linux和ARM平台上的安装步骤,以及安装中可能遇到的错误和解决方法。此外,还解释了时间戳的概念,包括RTP、NTP、PTS和DTS,并提供了Python代码示例,展示如何获取RTSP流中的各种时间戳。最后,文章还提供了一些附录,包括Python通过NTP同步获取时间的方法和使用PyAV访问网络视频流的技巧。
5184 4
PyAV学习笔记(一):PyAV简介、安装、基础操作、python获取RTSP(海康)的各种时间戳(rtp、dts、pts)
|
Python
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
使用Python的socket库实现客户端到服务器端的图片传输,包括客户端和服务器端的代码实现,以及传输结果的展示。
1403 3
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
|
JSON 数据格式 Python
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
本文介绍了如何使用Python的socket模块实现客户端到服务器端的文件传输,包括客户端发送文件信息和内容,服务器端接收并保存文件的完整过程。
1280 1
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
|
存储 Python
【免费分享编程笔记】Python学习笔记(二)
【免费分享编程笔记】Python学习笔记(二)
289 1
【免费分享编程笔记】Python学习笔记(二)
|
XML JSON API
如何使用Python将字典转换为XML
本文介绍了如何使用Python中的`xml.etree.ElementTree`库将字典数据结构转换为XML格式。通过定义递归函数处理字典到XML元素的转换,生成符合标准的XML文档,适用于与旧系统交互或需支持复杂文档结构的场景。示例代码展示了将一个简单字典转换为XML的具体实现过程。
481 1
|
关系型数据库 MySQL 数据库
Mysql学习笔记(四):Python与Mysql交互--实现增删改查
如何使用Python与MySQL数据库进行交互,实现增删改查等基本操作的教程。
357 1
|
Ubuntu Linux Python
Ubuntu学习笔记(六):ubuntu切换Anaconda和系统自带Python
本文介绍了在Ubuntu系统中切换Anaconda和系统自带Python的方法。方法1涉及编辑~/.bashrc和/etc/profile文件,更新Anaconda的路径。方法2提供了详细的步骤指导,帮助用户在Anaconda和系统自带Python之间进行切换。
806 1
|
索引 Python
【免费分享编程笔记】Python学习笔记(一)
【免费分享编程笔记】Python学习笔记(一)
207 1

热门文章

最新文章

推荐镜像

更多