xml.etree.ElmentTree不支持CDATA 的输出,但是支持Comment的输出。由于在项目中需要输出带有CDATA块的XML文本,参考Comment的做法,修改ElmentTree中的

_serialize_xml方法中。实现如下:
 

import xml.etree.ElementTree as ET

def CDATA(text=None):
element = ET.Element('![CDATA[')
element.text = text
return element

ET._original_serialize_xml = ET._serialize_xml

def _serialize_xml(write, elem, qnames, namespaces,short_empty_elements, **kwargs):

if elem.tag == '![CDATA[':
#write("\n<{}{}]]>\n".format(elem.tag, elem.text))
write("<%s%s]]>" % (elem.tag, elem.text))
if elem.tail:
write(ET._escape_cdata(elem.tail))
else:
return ET._original_serialize_xml(write, elem, qnames, namespaces,short_empty_elements, **kwargs)

ET._serialize_xml = ET._serialize['xml'] = _serialize_xml
 

测试代码:

text = """
<?xml version='1.0' encoding='utf-8'?>
<text>
This is just some sample text.
</text>
"""

e = ET.Element("data")
cdata = MyET.CDATA(text)
e.append(cdata)

#tests
print(ET.tostring(e,encoding="utf-8"))
 
结果:
b"<data><![CDATA[\n<?xml version='1.0' encoding='utf-8'?>\n<text>\nThis is just some sample text.\n</text>\n]]></data>"
 

xml.etree.ElementTree对CDATA的输出的更多相关文章

  1. python 之xml.etree.ElementTree

    Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. [注意]xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. 每个element对象都具有以下属性: ...

  2. Python 标准库之 xml.etree.ElementTree

    Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...

  3. [python 2.x] xml.etree.ElementTree module

    XML 文件:xmlparse.xml <?xml version="1.0" encoding="UTF-8" standalone="no& ...

  4. [python 学习] 使用 xml.etree.ElementTree 模块处理 XML

    ---恢复内容开始--- 导入数据(读文件和读字符串) 本地文件 country_data.xml <?xml version="1.0"?> <data> ...

  5. Python中xml.etree.ElementTree读写xml文件实例

    import osimport xml.etree.ElementTree as ET'''Python 标准库中,提供了6种可以用于处理XML的包,本文举实例说明第6种1.xml.dom2.xml. ...

  6. python标准库xml.etree.ElementTree的bug

    使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...

  7. self._raiseerror(v) File "D:\GameDevelopment\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror

    D:\BaiDuYun\Plist>python unpack_plist.py lobbyRelieveTraceback (most recent call last): File &quo ...

  8. python模块:xml.etree.ElementTree

    """Lightweight XML support for Python. XML is an inherently hierarchical data format, ...

  9. python xml.etree.ElementTree模块

    使用的XML文件如下:file.xml <?xml version="1.0"?> <data name="ming"> <cou ...

随机推荐

  1. RubyOnRails local_assigns

    http://api.rubyonrails.org/classes/ActionView/Template.html#method-i-local_assigns Returns a hash wi ...

  2. C#中用radio单选Repeater循环数据,js实现

    <asp:Repeater ID="rpt" runat="server"> <ItemTemplate> <tr data-id ...

  3. linux Mint 安装apache2

    sudo apt-get install apache2 y 启动apache2  /etc/init.d/apache2 restart 浏览器输入localhost看看是否访问正常 apache2 ...

  4. ThinkInside

    #sideBar,#blog_post_info_block { display: none;}#under_post_news { display: none;} /*评论框大小*/ #tbComm ...

  5. js抛物线动画——加入购物车动效

    参考文章:http://www.zhangxinxu.com/wordpress/2013/12/javascript-js-元素-抛物线-运动-动画/ parapola.js /*! * by zh ...

  6. rocketmq生产者和消费者

    1.生产者: package com.ebways.mq.test.mq; import com.alibaba.rocketmq.client.exception.MQClientException ...

  7. PHP单例模式

    //1.单例模式//目的:为了控制对象的数量(只能够有一个,相当于类的计划生育)//做法//1.将类的构造函数做成私有的//2.在类里面做了一个公有的函数来造对象//3.将该函数变为静态的//4.在函 ...

  8. Mac 下locate命令使用问题WARNING: The locate database (/var/db/locate.database) does not exist.

    想在Mac下使用locate时,提醒数据库没创建: WARNING: The locate database (/var/db/locate.database) does not exist. To ...

  9. glassfish配置

    最近刚接触glassfish,在此记录一些glassfish的使用. 1.下载完glassfish之后(在配置玩JAVA路径后),解压缩.找到glassfish目录下的bin文件夹,运行asadmin ...

  10. GitHub Windows客户端无法登录

    Windows 7系统,下载GitHub后始终无法登录,貌似填写的用户名和密码都没有提交服务器,直接客户端“验证”的. 解决办法: 下载 Microsoft .NET Framework 4.5 安装 ...