解析gbk格式带中文的xml

匿名 (未验证) 提交于 2019-12-03 00:39:02

方案

代码

 1 def parse_xml_node(node):  2     if len(node.getchildren()) == 0:  3         return node.text if node.text is not None else ‘‘  4     else:  5         node_dict = {}  6         for child in node.getchildren():  7             if child.tag in node_dict.keys():  8                 if not isinstance(node_dict[child.tag], list):  9                     node_dict[child.tag] = [node_dict[child.tag]] 10                 node_dict[child.tag].append(parse_xml_node(child)) 11             else: 12                 node_dict[child.tag] = parse_xml_node(child) 13         return node_dict 14  15 def parse_gbk_xml(filename): 16     import codecs 17     from xml.etree import ElementTree 18     with codecs.open(filename,r,encoding=gbk) as fp: 19         text = fp.read().replace(<?xml version="1.0" encoding="GBK"?>, <?xml version="1.0" encoding="UTF-8"?>) 20     xdata = {} 21     element = ElementTree.fromstring(text.encode(utf-8)) 22     xdata[element.tag] = parse_xml_node(element)

结果验证:

 1 # 文本内容  2 <?xml version="1.0" encoding="GBK"?>  3     <root>  4         <head>  5             <code>1</code>  6             <message>正确</message>  7             <value>320202</value>  8         </head>  9     </root> 10  11  12 # 解析结果 13 {root: {head: {message: u\u6b63\u786e, code: 1, value: 320202}}}

原文:https://www.cnblogs.com/elephanyu/p/9249085.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!