方案
1、以gbk格式读取xml文件为字符串,替换xml声明为utf-8格式
2、将字符串编码成utf-8格式,直接解析
代码
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‘}}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。