Python read XML error parsing--expaterror:not well-formed (invalid token)

Source: Internet
Author: User
Tags mail account

The contents of the XML file are stored in the name Login.xml as shown in the code:

<?XML version= "1.0" encoding= "Utf-8"?><Info>   <explain>126</explain>      <URL>Http://www.126.com</URL>      <NULLusername=""Password="">Please enter your e-mail account number first</NULL>      <Pawd_nullusername= "TESTINGWTB"Password=""></Pawd_null>      <User_nullusername=""Password= "a123456">      </User_null>      <Errorusername= "XXX"Password= "XXX"></Error></Info>

The Python source code itself is error-free:

#Coding =utf-8ImportXml.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.dataPrintusernamePrintPrompt_info

When parsing an XML file using the Xml.dom.mindom library, the following error is reported:

Traceback (most recent): File"D:\Python27\lianxidanma\xml11.py", Line 4,inch<module>Dom=xml.dom.minidom.parse ('D:\Python27\lianxidanma\login.xml') File"D:\Python27\lib\xml\dom\minidom.py", Line 1918,inchParsereturnexpatbuilder.parse (file) file"D:\Python27\lib\xml\dom\expatbuilder.py", line 924,inchParse Result=builder.parsefile (FP) File"D:\Python27\lib\xml\dom\expatbuilder.py", line 207,inchparsefile parser. Parse (buffer, 0) Expaterror: notWell-Formed (Invalid token): Line 5, column 36

In fact, this error is mainly "transcoding" the problem, if the XML file does not have Chinese, naturally be able to input the required data, but now the XML file has Chinese.

In general, when we do automated testing, we are accustomed to use TXT to edit XML files for data preservation, but after editing the XML file with TXT, it is customary to directly click on the save, the default encoding method is ANSI

The problem is in the encoding method, if we use UTF-8 encoding method to re-execute the script, then the program executes successfully, the correct output in Chinese:

Python 2.7.9 (default, Dec, 12:28:03) [MSC v.1500 64bit (AMD64)] on Win32type"Copyright","credits" or "license ()"  forMore information.>>> ================================ RESTART ================================>>>Traceback (most recent): File"D:\Python27\lianxidanma\xml11.py", Line 4,inch<module>Dom=xml.dom.minidom.parse ('D:\Python27\lianxidanma\login.xml') File"D:\Python27\lib\xml\dom\minidom.py", Line 1918,inchParsereturnexpatbuilder.parse (file) file"D:\Python27\lib\xml\dom\expatbuilder.py", line 924,inchParse Result=builder.parsefile (FP) File"D:\Python27\lib\xml\dom\expatbuilder.py", line 207,inchparsefile parser. Parse (buffer, 0) Expaterror: notWell-Formed (Invalid token): Line 5, column 36>>> ================================ RESTART =================== =============>>>Please enter your e-mail account number first>>>

Python read XML error parsing--expaterror:not well-formed (invalid token)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.