python自动化测试-读取XML文件报错
#coding:utf-8import xml.dom.minidom(或者from xml.dom import minidom)
File=xml.dom.minidom.parse("Class.xml") #这边的文件我是放在其他地方,所以路径也试过比如:File=xml.dom.minidom.parse(" E:\自动化测试\自动化测试练习\我要自学网-自动化教程\practise、Class.xml")
就这个文件读取报错:
E:\pyCharm\users\venv\Scripts\python.exe E:/pyCharm/users/object/Read_file001.py
Traceback (most recent call last):
File "E:/pyCharm/users/object/Read_file001.py", line 29, in <module>
File=xml.dom.minidom.parse("Class.xml")
File "C:\Programs\Python\Python37-32\lib\xml\dom\minidom.py", line 1958, in parse
return expatbuilder.parse(file)
File "C:\Programs\Python\Python37-32\lib\xml\dom\expatbuilder.py", line 911, in parse
result = builder.parseFile(fp)
File "C:\Programs\Python\Python37-32\lib\xml\dom\expatbuilder.py", line 207, in parseFile
parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 28, column 34
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<Class>
<student>
<name>Jack</name>
<age>33</age>
<city>beijing</city>
</student>
<student>
<name>Bob</name>
<age>54</age>
<city>shanghai</city>
</student>
<student>
<name>Judi</name>
<age>18</age>
<city>shenzhen</city>
</student>
<teacher>
<name>Blus</name>
<age>62</age>
<city>guangzhou</city>
</teacher>
<account>
<login username="student",password="123456"/>
<login username="teacher",password="888888"/>
</account>
</Class>
各位大神,我这是按视频里的来写的,用的PYcharm,python3.7版本的,为什么查找了网上资料说是编码的问题,可以把XML另存为UTF8还是一样报这错,这到底是哪里出了问题呢,求解,谢谢给为无私的大神帮忙解答 已经解决,不是编码的问题,是xml中最后的标签login的问题,不要搞这么写,应该写成<login></login>格式,希望遇到同类问题的朋友可以参考:) <account>
<login username="student",password="123456"/>
<login username="teacher",password="888888"/>
</account>
这边更改后却出现了新问题改成<login></login>后
logins=root.getElementsByTagName('login')
usernames=logins.getAttribute('username');这边取不到值,返回为空
页:
[1]