def write_city_to_xml(dic=None, to_path=None): if dic is None or to_path is None: return None root_node = etree.Element('root') root_node.text = "\n\t" city_node = etree.SubElement(root_node, 'citys') comment_node = etree.Comment("\n城市信息\n") comment_node.tail = "\n\t" city_node.append(comment_node) city_node.text = "\n\t" + stringer.dict_to_json(dic, "\t") + u'\n' city_node.tail = "\n" city_tree = etree.ElementTree(root_node) city_tree.write(to_path, pretty_print=True, xml_declaration=True, encoding='utf-8')
def write_student_to_xml(dic=None, to_path=None): if dic is None or to_path is None: return None doc = Document() root_node = doc.createElement("root") doc.appendChild(root_node) stu_node = doc.createElement("students") root_node.appendChild(stu_node) note_node = doc.createComment("\n\t学生信息表\n\t\"id\" : [名字, 数学, 语文, 英文]\n\t") stu_node.appendChild(note_node) # data = json.dumps(dic, ensure_ascii=False, indent=1) dic_node = doc.createTextNode(stringer.dict_to_json(dic, "\t\t")) stu_node.appendChild(dic_node) file = open(to_path, "w") file.write(doc.toprettyxml()) # doc.writexml(file,' ',' ','\n','utf-8') file.close()