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

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

Python列表(list)所有元素的同一操作解析

Python列表(list)所有元素的同一操作解析

针对很普遍的每个元素的操作会遍历每个元素进行操作。 这里给出了几种写法,列表每个元素自增等数学操作同理; 示例:整形列表ilist加1个数、元素类型转字符串: ilist = [1,...

python多进程实现进程间通信实例

python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程。Python提供了非常好用的多进程包multiprocessi...

Flask框架学习笔记之使用Flask实现表单开发详解

Flask框架学习笔记之使用Flask实现表单开发详解

本文实例讲述了使用Flask实现表单开发。分享给大家供大家参考,具体如下: <!DOCTYPE html> <html lang="en"> <head...

Win10 安装PyCharm2019.1.1(图文教程)

Win10 安装PyCharm2019.1.1(图文教程)

使用Python进行开发时,已经安装好Python,我们可以在终端使用Python,也可以用文本编辑器编写代码后然后用Python执行代码,但这些都不是很方便,也不是很智能和不够友好,需...

Python实现的排列组合、破解密码算法示例

本文实例讲述了Python实现的排列组合、破解密码算法。分享给大家供大家参考,具体如下: 排列组合(破解密码) 1.排列 itertools.permutations(iterabl...