domImp = minidom.getDOMImplementation() dom = domImp.createDocument(None,'doc',None) root = dom.documentElement #建立元素 section_node = dom.createElement('section') #设置属性 section_node.setAttribute('url','http://www.sina.com.cn/') section_node.setAttribute('title','这是标题') content_node = dom.createElement('content') #建立CData元素 cdata = dom.createCDATASection('<b>some</b>text'); #附件子节点 content_node.appendChild(cdata) section_node.appendChild(content_node) root.appendChild(section_node) print dom.toprettyxml()