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 ================================
>>> 请先输入您的邮箱帐号
>>>
Python读取xml报错解析--ExpatError: not well-formed (invalid token)的更多相关文章
- 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence
python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...
- DataSet 读取xml 报错有非法字符
private void Bind() { string strLogPath = ConfigurationSettings.AppSettings["LOG_PATH"].To ...
- 【Python】Python读取文件报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 20: illegal multibyte sequence
环境描述 text.txt 今天的天气不错 是个皻的选择 读取文件的代码 #!/usr/bin/python #-*- coding:UTF-8 -*- f = open(r'D:\Python\Py ...
- python读取文件报错:pandas.errors.ParserError: iterator should return strings, not bytes (did you open the file in text mode?)
python 读取csv文件报错问题 import csv with open('E:/Selenium2script/DDT模块/test.csv','rb') as f: readers = cs ...
- web.xml报错:cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'. One of '{"http://java.sun.com/xml/ns/javaee":init-param}' is expected.
在写xml的时候又一次总是报cvc-complex-type.2.4.a: Invalid content was found starting with element 错误,还出现小红叉,在网上找 ...
- Java使用dom4j读取xml时报错:org.dom4j.DocumentException: Error on line 2 of document : Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence
1.Java使用dom4j读取xml时报错: org.dom4j.DocumentException: Error on line 2 of document : Invalid byte 2 of ...
- (转)解决dubbox-demo-provider.xml报错的问题:提示Failed to read schema document
背景:在eclipse项目中,经常会遇到xml文件提示找不到.xsd文件的情况.很有必要弄清xsd文件的加载过程! 1 解决过程 dubbo-demo-provider导入eclipse后dubbox ...
- java -- SSM配置完成后,能访问jsp文件不能访问html文件,报错解析
SSM配置完成后,能访问jsp文件不能访问html文件,报错解析 在确保路径没有任何问题的,情况下,相同的页面,jsp能够正常访问,html却不能正常访问(404). 解决方法: 在web.xml中添 ...
- python读取xml文件
关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...
随机推荐
- Effective Java 读书笔记之八 异常
一.只针对异常的情况才使用异常 1.类具有状态相关的方法时,可采用状态测试方法和可识别的返回值两个策略. 二.对可恢复的情况使用受检异常,对编程错误使用运行时异常 1.期望调用者能够适当恢复的情况,应 ...
- dispaly:table-cell,inline-block,阐述以及案例
display:table 此元素会作为块级表格来显示(类似 <table>),表格前后带有换行符.dispaly:table-row 此元素会作为一个表格行显示(类似 <tr> ...
- OpenCV进阶之路:一个简化的视频摘要程序
一.前言 视频摘要又称视频浓缩,是对视频内容的一个简单概括,先通过运动目标分析,提取运动目标,然后对各个目标的运动轨迹进行分析,将不同的目标拼接到一个共同的背景场景中,并将它们以某种方式进行组合.视频 ...
- PHP输出控制(Output Control)函数
ob_start 此函数将打开输出缓冲.当输出缓冲激活后,脚本将不会输出内容(除http标头外),相反需要输出的内容被存储在内部缓冲区中. 内部缓冲区的内容可以用 ob_get_contents() ...
- bufferedimage 转换成 inputstream并保存文件
BufferedImage img = removeBackgroud(file);//去除重影 //bufferedimage 转换成 inputstream ByteArrayOutputStre ...
- 浅探委托(delegate)和事件(event)
.NET Framework通过委托提供了一种回调函数机制. internal delegate void FeedBack(Int32 value); 内部委托FeedBack的声明,一个委托要指定 ...
- 7.31 签到,js 全局预处理笔记
js 解析与执行过程: 一.全局: 1.预处理阶段 : 1.LexicalEnviroment === window {1.预处理 var | 2.function xxx //预处理申明的 ...
- Portal
https://chenliang0571.wordpress.com/2013/12/08/openwrt-wifidog-wifi-hotspots/http://www.h3c.com.cn/M ...
- Selenium2(webdirver)入门之环境搭建(Java版)
一.开发环境: 1.JDK1.6 2.Eclipse:Version: Kepler Service Release 1,下载地址:http://www.eclipse.org/downloads/ ...
- 使用logrotate管理nginx日志文件
本文转载自:http://linux008.blog.51cto.com/2837805/555829 描述:linux日志文件如果不定期清理,会填满整个磁盘.这样会很危险,因此日志管理是系统管理员日 ...