在使用既有的python的三種方法(SAX,DOM,以及ElementTree)解析XML時,若xml文件裏有中文,如果不做特殊處理,通常會報錯。以下代碼是解決這個問題的一種方法:
def getURLfromXML(filePath):
import xml.dom.minidom
#f = open(r'E:\download\可汗學院\線性代數\2_M82ICR1D9_M83C7VICB.xml', "r")
f = open(filePath, "r")
r = f.read()
text = str(r.encode('utf-8'), encoding = "utf-8")
#print(text)
# 使用minidom解析器打開 XML 文檔
DOMTree = xml.dom.minidom.parseString(text)
# 使用minidom解析器打開 XML 文檔
#DOMTree = xml.dom.minidom.parse(r'E:\download\可汗學院\線性代數\2_M82ICR1D9_M83C7VICB.xml')
collection = DOMTree.documentElement
#if collection.hasAttribute("shelf"):
# print("Root element : %s" % collection.getAttribute("shelf"))
# 在集合中獲取sub
subs = collection.getElementsByTagName("sub")
# 打印每個字幕的詳細信息
for sub in subs:
#name = sub.getElementsByTagName('name')[0]
#print(filePath + " Name: %s" % name.childNodes[0].data)
url = sub.getElementsByTagName('url')[0]
print(filePath + " Url: %s" % url.childNodes[0].data)