欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python基于xml parse实现解析cdatasection数据

程序员文章站 2023-11-23 18:54:04
本文实例讲述了python基于xml parse实现解析cdatasection数据的方法,分享给大家供大家参考。 具体实现方法如下: from xml.dom...

本文实例讲述了python基于xml parse实现解析cdatasection数据的方法,分享给大家供大家参考。

具体实现方法如下:

from xml.dom.minidom import * 
 
implementation = domimplementation() 
 
print "core:%s" % implementation.hasfeature('core', '2.0') 
print "events:%s" % implementation.hasfeature('events', '2.0') 
print "traversal:%s" % implementation.hasfeature('traversal', '2.0') 
print "views:%s" % implementation.hasfeature('views', '2.0') 
print "features:%s" % implementation._features 
     
dom = parse("result.xml")  
domroot = dom.documentelement 
print domroot 
print domroot.nodetype 
print "element_node:%s " % dom.element_node 
print "attribute_node:%s " % dom.attribute_node 
 
children = domroot.childnodes 
for child in children: 
  print "child_get_tagname:%s" % child._get_tagname() 
  print "child_get_localname:%s" % child._get_localname() 
  print "child.haschildnodes:%s" % child.haschildnodes() 
  if child._get_tagname() == "files_rg": 
    files = child._get_childnodes() 
    for file in files: 
      if file.nodetype == dom.element_node: 
        for node in file._get_childnodes(): 
          print "node.childnodes:%s ",node._get_childnodes() 
          for cdatasection in node._get_childnodes(): 
            if cdatasection.nodetype == dom.cdata_section_node: 
              print "cdatasection._get_data:%s %s " % (cdatasection._get_localname(),cdatasection._get_data()) 

希望本文所述对大家的python程序设计有所帮助。