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)