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, indom=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, indom=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 ================================>>> 请先输入您的邮箱帐号>>>