Python讀取xml報錯解析--ExpatError: not well-formed (invalid token)

來源:互聯網
上載者:User

標籤:

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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.