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判断字符串或者集合是否为空的实例

最近在看《Effective Python》,里面提到判断字符串或者集合是否为空的原则,原文如下: Don't check for empty values (like [] or ''...

Python搭建代理IP池实现接口设置与整体调度

Python搭建代理IP池实现接口设置与整体调度

接口模块需要用 API 来提供对外服务的接口,当然也可以直接连数据库来取,但是这样就需要知道数据库的连接信息,不太安全,而且需要配置连接,所以一个比较安全和方便的方式就是提供一个 Web...

python类继承与子类实例初始化用法分析

本文实例讲述了python类继承与子类实例初始化用法。分享给大家供大家参考。具体分析如下: [ 先贴参考书籍原文(中文英文对照)] __init__方法介绍: If a base cla...

pytorch下使用LSTM神经网络写诗实例

在pytorch下,以数万首唐诗为素材,训练双层LSTM神经网络,使其能够以唐诗的方式写诗。 代码结构分为四部分,分别为 1.model.py,定义了双层LSTM模型 2.data.py...

python将邻接矩阵输出成图的实现

python将邻接矩阵输出成图的实现

利用networkx,numpy,matplotlib,将邻接矩阵输出为图形。 1,自身确定一个邻接矩阵,然后通过循环的方式添加变,然后输出图像 import networkx as...