python xml操作

2017/7/30 posted in  python

生成xml数据

from xml.dom.minidom import Document
doc = Document()
chapterinfo = doc.createElement('chapterinfo')
doc.appendChild(chapterinfo)

bookid = doc.createElement('bookid')
title_text = doc.createCDATASection('12333')

bookid.appendChild(title_text)
chapterinfo.appendChild(bookid)
    
print doc.toxml('utf-8')
<?xml version="1.0" encoding="utf-8"?>
<chapterinfo>
    <bookid>
        <![CDATA[12333]]>
    </bookid>
</chapterinfo>

增加文本节点

# coding=utf-8
from xml.dom.minidom import Document

# 增加Cdata节点
def appendCdataNode(xmlobj, title, text):
    doc = Document()
    tmp = doc.createElement(title)
    text = str2unicode(text)
    tmp_txt = doc.createCDATASection(text)
    tmp.appendChild(tmp_txt)
    xmlobj.appendChild(tmp)

# 增加普通text节点
def appendTextNode(xmlobj, title, text):
    doc = Document()
    tmp = doc.createElement(title)
    if not isinstance(text, str):
        try:
            text = str(text)
        except Exception as e:
            print e
            return
    text = str2unicode(text)
    tmp_txt = doc.createTextNode(text)
    tmp.appendChild(tmp_txt)
    xmlobj.appendChild(tmp)

# str转成unicode,避免在组装xml对象时出错
def str2unicode(text):
    if type(text) == str:
        return text.decode('utf-8')
    elif type(text) == unicode:
        return text
    else:
        print "str2unicode failed, text type is not str or unicode, type=%s" % type(text)
        return None

# 测试代码
if __name__ == '__main__':
    doc = Document()
    book = doc.createElement('book')
    appendTextNode(book, "bookname", "热")
    appendTextNode(book, "viewcount", 10003)
    appendCdataNode(book, "description", "这不是一本黄书")
    doc.appendChild(book)
    
    print doc.toprettyxml()

结果

<?xml version="1.0" ?>
<book>
    <bookname>热</bookname>
    <viewcount>10003</viewcount>
    <description><![CDATA[这不是一本黄书]]></description>
</book>