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 文件. 什么是 ...
随机推荐
- Java设计模式 之 工厂方法模式
1. 使用设计模式的好处:可提高代码的重复性,让代码更容易被他人理解,保证代码的可靠性. 2. 工厂模式定义:就是创建一个工厂类来创建你需要的类,工厂模式包括工厂模式和抽象工厂模式,抽象工厂模式是工厂 ...
- 安装JBPM6运行环境(JBPM6学习之二)
安装Eclipse插件成功后,需要配置JBPM6的运行环境: 1. 第一步先将下载的jbpm6目录中的“jbpm-6.0.1.Final-bin.zip”找到,并解压缩到D盘根目录备用. 2. 第二步 ...
- http基础实战
1.需求 了解http的基础知识,能看懂chrome下网络的情况 2.前置知识 下面是tcp/ip协议的一些东西,今天就只用了解应用层的http就够了. 3.http是什么 我们在网上浏览网页,会发送 ...
- 1.4---字符串空格变成20%(CC150)
import CtCILibrary.AssortedMethods; public class Question { // Assume string has sufficient free spa ...
- PyQt4软件打包成exe文件
使用py2exe进行打包 例: from distutils.core import setupimport py2exeimport sys sys.argv.append('py2exe') py ...
- Extjs String转Json
var jsonStr= '{ "name": "zhangsan", "age": "18" }'; var json ...
- yuv转bmp
#ifdef _INTERFACE_H #error _INTERFACE_H has be exsisted #else #define _INTERFACE_H #include "st ...
- nova 虚拟机迁移
ref: 官方文档很给力: nova 迁移有以下类型: Non-live migration (sometimes referred to simply as ‘migration’). The in ...
- vim python设置
http://www.cnblogs.com/Leo-Forest/archive/2012/04/06/2435237.html http://linux-wiki.cn/wiki/zh-hans/ ...
- oracle11g 连接问题
一.The Network Adapter could not establish the connection 状态: 失败 -测试失败: IO 错误: The Network Adapter c ...