Python訪問.xml文件時,報文件解析錯誤的類型之一


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

4.查看報錯,並分析原來xml的第一行聲明沒有閉合。少寫了一個'?'導致的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM