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 文件. 什么是 ...
随机推荐
- CSU 1113 Updating a Dictionary(map容器应用)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...
- C#高级编程笔记 Day 2, 2016年8月 31日 构造函数
1.构造函数: 实例构造函数(只要创建了对象,就会执行)一般使用 this 关键字区分成员字段和同名的参数.可以把构造函数定义为private 或 protected .这样不相关的类也不能访问他们. ...
- 剑指Offer 合并两个排序的链表
题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 思路: 用2个新节点,一个用来存放新链表的头节点,另一个用来移动.当p1,p2有一个到尾部的 ...
- php批量下载文件
最近用codeigniter开发一个图片网站,发现单文件下载很容易实现,批量下载的话,就有点麻烦. 普通php下载比较简单,比如我封装的一个函数: function shao_download($fi ...
- 校赛E题递归形式
#include<stdio.h> #include<string.h> using namespace std; typedef long long ll; ][]; ,,, ...
- __getattr__ 与动态属性
直接上代码 >>> class Test(object): ... def __getattr__(self,attr_name): ... setattr(self, attr_n ...
- 开始做POI啦...
库 为了效率搞了这么一个库: 现在版本号1.14(一月十四日更新版本囧..) http://pan.baidu.com/s/1c0SoGfu [source] http://pan.baidu.com ...
- discuz后台开发常用函数
showsetting()表单显示 返回值:无 参数: $setname - 指定输出标题,如:setting_basic_bbname, 自动匹配描述文字为:setting_basic_bbname ...
- 【GoLang】GoLang map 非线程安全 & 并发度写优化
Catena (时序存储引擎)中有一个函数的实现备受争议,它从 map 中根据指定的 name 获取一个 metricSource.每一次插入操作都会至少调用一次这个函数,现实场景中该函数调用更是频繁 ...
- PHP入门笔记
PHP是一种创建动态交互性站点的强有力的服务器端脚本语言.PHP其代码可以直接嵌入HYML代码.PHP语法非常类似于Perl和C,常常搭配Apache一起使用. 1.PHP是指超文本预处理器(Hype ...