博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python读取xml报错解析--ExpatError: not well-formed (invalid token)
阅读量:4982 次
发布时间:2019-06-12

本文共 2204 字,大约阅读时间需要 7 分钟。

xml文件内容如代码所示存入的名字为login.xml:

126
http://www.126.com
请先输入您的邮箱帐号

Python源代码代码本身是没有错误的:

#coding =utf-8import xml.dom.minidomdom=xml.dom.minidom.parse('D:\Python27\lianxidanma\login.xml')root = dom.documentElementlogins=root.getElementsByTagName('null')username=logins[0].getAttribute("username")password=logins[0].getAttribute("password")prompt_info = logins[0].firstChild.dataprint usernameprint prompt_info

 

使用xml.dom.mindom库解析xml文件时,报如下错误:

Traceback (most recent call last):  File "D:\Python27\lianxidanma\xml11.py", line 4, in 
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 win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> Traceback (most recent call last):  File "D:\Python27\lianxidanma\xml11.py", line 4, in 
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 ================================>>> 请先输入您的邮箱帐号>>>

 

转载于:https://www.cnblogs.com/HCT118/p/4507603.html

你可能感兴趣的文章