python xml dom clone_python的xml.dom学习笔记

#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 "

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']

None

1 None link

link

[]

None None

http://www.w3.org/1999/xhtml

--------------------

link

============================================================

[, , ]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值