zoukankan      html  css  js  c++  java
  • 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 ================================
    >>> 
    
    请先输入您的邮箱帐号
    >>> 
  • 相关阅读:
    [.Net 5.0] 笔记
    java计算两个字符串日期的相差天数
    记一个使用fyne-cross编译的坑
    windows gcc 遇到的问题解决
    异步处理在支付环节的应用
    怎么修复 "replaceAll is not a function" JavaScript Error?
    Js生成随机数 生成随机字符串的5种方法
    json-bigint的介绍和使用-解决Javascript的有关大整数问题
    国产操作系统——银河麒麟V10 SP1使用小结
    【转载】 银河麒麟V10系统安装U盘制作
  • 原文地址:https://www.cnblogs.com/HCT118/p/4507603.html
Copyright © 2011-2022 走看看