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  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,如需转载请自行联系原作者
相关文章
|
2月前
|
存储 C语言 Python
【Python】学习笔记day3
【Python】学习笔记day3
27 1
|
1月前
|
前端开发 安全 JavaScript
Python的Flask框架的学习笔记(前后端变量传送,文件上传,网页返回)内含实战:实现一个简单的登录页面
Python的Flask框架的学习笔记(前后端变量传送,文件上传,网页返回)内含实战:实现一个简单的登录页面
|
1天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
6 1
|
16天前
|
移动开发 JavaScript 前端开发
webgl学习笔记3_javascript的HTML DOM
webgl学习笔记3_javascript的HTML DOM
19 0
webgl学习笔记3_javascript的HTML DOM
|
17天前
|
XML 数据格式 Python
【代码片段】【Python】XML 字符串格式化打印
【代码片段】【Python】XML 字符串格式化打印
17 0
|
23天前
|
Python
基于Django的Python应用—学习笔记—功能完善
基于Django的Python应用—学习笔记—功能完善
|
23天前
|
存储 数据库 数据安全/隐私保护
基于Django的Python应用——学习笔记
基于Django的Python应用——学习笔记
|
28天前
|
XML JavaScript API
「Python系列」Python XML解析
在Python中,解析XML文件通常使用内置的`xml.etree.ElementTree`模块,它提供了一个轻量级、高效的方式来解析XML文档。此外,还有其他的第三方库,如`lxml`和`xml.dom`,它们提供了更多的功能和灵活性。
14 0
|
2月前
|
存储 C语言 芯片
【Python】学习笔记day1
【Python】学习笔记day1
34 1
|
2月前
|
算法 搜索推荐 测试技术
python排序算法及优化学习笔记1
python实现的简单的排序算法,以及算法优化,学习笔记1
35 1