• 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:Python27lianxidanmalogin.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:Python27lianxidanmaxml11.py", line 4, in <module>
        dom=xml.dom.minidom.parse('D:Python27lianxidanmalogin.xml')
      File "D:Python27libxmldomminidom.py", line 1918, in parse
        return expatbuilder.parse(file)
      File "D:Python27libxmldomexpatbuilder.py", line 924, in parse
        result = builder.parseFile(fp)
      File "D:Python27libxmldomexpatbuilder.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:Python27lianxidanmaxml11.py", line 4, in <module>
        dom=xml.dom.minidom.parse('D:Python27lianxidanmalogin.xml')
      File "D:Python27libxmldomminidom.py", line 1918, in parse
        return expatbuilder.parse(file)
      File "D:Python27libxmldomexpatbuilder.py", line 924, in parse
        result = builder.parseFile(fp)
      File "D:Python27libxmldomexpatbuilder.py", line 207, in parseFile
        parser.Parse(buffer, 0)
    ExpatError: not well-formed (invalid token): line 5, column 36
    >>> ================================ RESTART ================================
    >>> 
    
    请先输入您的邮箱帐号
    >>> 
  • 相关阅读:
    关于一位程序员入门的面试经验
    Outpro的博客测试
    优先队列
    linux (centos 6.2)在输入查询或者操作命令时提示-bash: fork: cannot allocate memory
    win10下JDK环境变量
    Mac OS如何安装IDEA
    解决下载github代码慢的问题
    vue 模板语法之指令
    vue的基本介绍以及第一个程序
    消息中间的几大应用场景
  • 原文地址:https://www.cnblogs.com/HCT118/p/4507603.html
Copyright © 2020-2023  润新知