標籤:
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-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 <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 win32Type "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 ================================>>> 請先輸入您的郵箱帳號>>>
Python讀取xml報錯解析--ExpatError: not well-formed (invalid token)