Python读取xml报错解析--ExpatError: not well-formed (invalid token)

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-8
import xml.dom.minidom

dom=xml.dom.minidom.parse(‘D:\Python27\lianxidanma\login.xml‘)
root = dom.documentElement
logins=root.getElementsByTagName(‘null‘)
username=logins[0].getAttribute("username")
password=logins[0].getAttribute("password")
prompt_info = logins[0].firstChild.data

print username
print 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 win32
Type "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 ================================
>>> 

请先输入您的邮箱帐号
>>> 
时间: 2024-10-13 01:45:21

Python读取xml报错解析--ExpatError: not well-formed (invalid token)的相关文章

Python 读取csv报错编码问题 : UnicodeDecodeError: &#39;utf-8&#39; codec can&#39;t decode byte 0x87 in position 10: invalid start byte

1.报错信息 UnicodeDecodeError: 'utf-8' codec can't decode byte 0x87 in position 10: invalid start byte 2.解决方法 查找相关资料,发现一个棒棒的解决方法: 首先把csv文件以记事本的格式打开,默认保存是ANSI编码,我们应该手动修改成utf-8 ===>(如果记事本打开后是乱码,我们可以把csv数据粘贴到记事本上后保存) 3.接下来可以愉快的读文件咯 with csv23.open_csv(r'C:\

DataSet 读取xml 报错有非法字符

private void Bind() { string strLogPath = ConfigurationSettings.AppSettings["LOG_PATH"].ToString(); if (strLogPath.Length > 0 && strLogPath[strLogPath.Length - 1] != '\\') strLogPath += '\\'; string strXMLUrl = String.Format("{0}

[Python] 运行Flask, 报错:UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xc4 in position 33: ordinal not in range(128)

运行时报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 33: ordinal not in range(128) Python2.7在Windows上一个bug!!! 解决方法: 参考官方patch: http://bugs.python.org/file19332/9291a.patch 如下代码:一,加入from itertools import count: 二,修改 def enum_type

(转)解决dubbox-demo-provider.xml报错的问题:提示Failed to read schema document

背景:在eclipse项目中,经常会遇到xml文件提示找不到.xsd文件的情况.很有必要弄清xsd文件的加载过程! 1 解决过程 dubbo-demo-provider导入eclipse后dubbox-demo-provider.xml报错详情: Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be

python框架Scrapy报错TypeError: &#39;float&#39; object is not iterable解决

原因是:Twisted版本高了. 解决办法: 只要把Twisted库降级到16.6.0即可: 1 pip3 install Twisted==16.6.0 2 3 注:Twisted16.6.0安装后,会自动卸载高版本的Twisted python框架Scrapy报错TypeError: 'float' object is not iterable解决

python中文注释报错问题

错误信息: SyntaxError: Non-ASCII character '\xe4' in file... 解决办法: 在文件第一行或第二行添加:# -*- coding: utf-8 -*- 参考:http://blog.csdn.net/chen861201/article/details/7706535 python中文注释报错问题,布布扣,bubuko.com

python 读取文件时报错UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0x80 in position 205: illegal multibyte sequence

python读写txt文件转化成excel文件 python读取文件时提示"UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence" 解决办法1. FILE_OBJECT= open('order.log','r', encoding='UTF-8') 解决办法2. FILE_OBJECT= open('order.log','rb') pyth

Windows下使用pip安装python包是报错-UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xcb in position 0

先交待下开发环境: 操作系统:Windows 7 Python版本:2.7.9 Pip版本:6.1.1 其他环境忽略 在windows下使用pip下载python包,出现如下错误 [plain] view plain copy Collecting xxxxxx Exception: Traceback (most recent call last): File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\basecommand.

python安装locustio报错error: invalid command &#39;bdist_wheel&#39;的解决方法

locust--scalable user load testing tool writen in Python(是用python写的.规模化.可扩展的测试性能的工具) 安装locustio需要的环境条件是:Python2.6+,但是不支持Python3. 今天在cmd里运行pip install locustio,报错提示:error: invalid command 'bdist_wheel'. 原因:pip和setuptools的版本较低. 解决方案:升级pip和setuptools. 一