700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python的xml.dom学习笔记

python的xml.dom学习笔记

时间:2020-06-18 13:35:36

相关推荐

python的xml.dom学习笔记

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

#coding=utf-8#解析xml文件中的所有的link标签from xml.dom import minidomfrom xml.dom.minidom import getDOMImplementationdoc=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.parentNodeprint node.prefixprint node.nodeType,node.nodeValue,node.nodeNameprint node.localNameprint node.childNodesprint node.firstChild,node.lastChildprint node.attributesprint node.namespaceURIprint node.nextSiblingprint "--"*10print node.tagNameprint "==="*20impl=getDOMImplementation()newdoc=impl.createDocument(None , "some_tag", None)top_element=newdoc.documentElementnode1=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=" /css/common.css " />< link type=" text/css " rel=" stylesheet " href=" /Skins/kubrick/style.css " />< link type=" text/css " rel=" stylesheet " href=" /css/common2.css " />< link type=" text/css " rel=" stylesheet " href=" /css/shCore.css " />< link type=" text/css " rel=" stylesheet " href=" /css/shThemeDefault.css " />< link type=" application/rss+xml " rel=" alternate " href=" /rollenholt/rss " />< link type=" application/rsd+xml " rel=" EditURI " href=" /rollenholt/rsd.xml " />< link type=" application/wlwmanifest+xml " rel=" wlwmanifest " href=" /rollenholt/wlwmanifest.xml " />通过另外一种方式获得link标签text/css stylesheet /css/common.csstext/css stylesheet /Skins/kubrick/style.csstext/css stylesheet /css/common2.csstext/css stylesheet /css/shCore.csstext/css stylesheet /css/shThemeDefault.cssapplication/rss+xml alternate /rollenholt/rssapplication/rsd+xml EditURI /rollenholt/rsd.xmlapplication/wlwmanifest+xml wlwmanifest /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>None1 None linklink[]None None<xml.dom.minidom.NamedNodeMap object at 0x01B4D648>/1999/xhtml<DOM Text node "u'\n'">--------------------link============================================================[<DOM Text node "'node1'">, <DOM Text node "'node3'">, <DOM Text node "'node2'">]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。