Python读取xml报错解析--ExpatError: not well-formed (invalid token)
xml文件内容如代码所示存入的名字为login.xml:
<?xml version="1.0" encoding="utf-8"?> <info> <explain>126</explain> <url>http://www.126.com</url> <null username="" password="">请先输入您的邮箱帐号</null> <pawd_null username="testingwtb" password=""></pawd_null> <user_null username="" password="a123456"> </user_null> <error username="xxx" password="xxx"></error> </info>
Python源代码代码本身是没有错误的:
#coding =utf-8 import xml.dom.minidom dom=xml.dom.minidom.parse(‘D:\Python27\lianxidanma\login.xml‘) root = dom.documentElement logins=root.getElementsByTagName(‘null‘) username=logins[0].getAttribute("username") password=logins[0].getAttribute("password") prompt_info = logins[0].firstChild.data print username print prompt_info
使用xml.dom.mindom库解析xml文件时,报如下错误:
Traceback (most recent call last): File "D:\Python27\lianxidanma\xml11.py", line 4, in <module> dom=xml.dom.minidom.parse(‘D:\Python27\lianxidanma\login.xml‘) File "D:\Python27\lib\xml\dom\minidom.py", line 1918, in parse return expatbuilder.parse(file) File "D:\Python27\lib\xml\dom\expatbuilder.py", line 924, in parse result = builder.parseFile(fp) File "D:\Python27\lib\xml\dom\expatbuilder.py", line 207, in parseFile parser.Parse(buffer, 0) ExpatError: not well-formed (invalid token): line 5, column 36
其实报这个错误主要还是“转码”的问题,如果xml文件中没有中文,自然能够输入所需要的数据,但是现在xml文件中有中文。
一般情况我们在做自动化测试的时候,习惯用txt来编辑xml文件进行数据保存,但是在用txt编辑完xml文件后,都习惯性的直接点击保存,默认保存的编码方式是ANSI
问题就出在编码方式,如果我们用UTF-8的编码方式保存后,重新执行脚本,那么程序执行成功,正确输出中文:
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> Traceback (most recent call last): File "D:\Python27\lianxidanma\xml11.py", line 4, in <module> dom=xml.dom.minidom.parse(‘D:\Python27\lianxidanma\login.xml‘) File "D:\Python27\lib\xml\dom\minidom.py", line 1918, in parse return expatbuilder.parse(file) File "D:\Python27\lib\xml\dom\expatbuilder.py", line 924, in parse result = builder.parseFile(fp) File "D:\Python27\lib\xml\dom\expatbuilder.py", line 207, in parseFile parser.Parse(buffer, 0) ExpatError: not well-formed (invalid token): line 5, column 36 >>> ================================ RESTART ================================ >>> 请先输入您的邮箱帐号 >>>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。