1.访问Class_info.xml文件的代码,如下:
from xml.dom import minidom
#打开xml文件
dom=minidom.parse('D:\\Python_script\\file\\Class_info.xml')
#获取文档对象元素
root=dom.documentElement
#打印元素节点名称,节点值,节点类型
print(root.nodeName)
print(root.nodeValue)
print(root.nodeType)
2.Class_info.xml的内容,如下:
<?xml version="1.0" encoding="utf-8">
<Class>
<student>
<name>Jack</name>
<age>22</age>
<city>United station</city>
</student>
<student>
<name>平野美宇</name>
<age>17</age>
<city>Japan</city>
</student>
<teacher>
<name>陈正</name>
<age>20</age>
<city>中国</city>
</teacher>
<login username='student' password='123456'/>
<login username='teacher' password='123456'/>
</Class>
3.将2中Class_info.xml的第一行xml声明写错时,报如下错误:
"D:\Program Files\Python36\python.exe" D:/Python_script/Base_Class/Xml_file.py
Traceback (most recent call last):
File "D:/Python_script/Base_Class/Xml_file.py", line 3, in <module>
dom=minidom.parse('D:\\Python_script\\file\\Class_info.xml')
File "D:\Program Files\Python36\lib\xml\dom\minidom.py", line 1958, in parse
return expatbuilder.parse(file)
File "D:\Program Files\Python36\lib\xml\dom\expatbuilder.py", line 911, in parse
result = builder.parseFile(fp)
File "D:\Program Files\Python36\lib\xml\dom\expatbuilder.py", line 211, in parseFile
parser.Parse("", True)
xml.parsers.expat.ExpatError: unclosed token: line 1, column 0