马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 qiangqiang1 于 2020-5-27 08:31 编辑
已经解决,是XML格式有问题,代码并没有问题
===========================================================================import xml.sax
class FireStormHandler(xml.sax.ContentHandler):
def __init__(self):
self.CurrentData = ''
self.Test_Item = ''
self.Upper_Limit = ''
self.Lo1wer_Limit = ''
self.Measure_Data = ''
self.Measure_Time = ''
self.Pass = ''
#元素開始事件處理
def startElement(self, tag, attributes):
self.CurrentData = tag
if tag == "TESTITEM":
print ("*****Movie*****")
# 元素結束事件處理
def endElement(self, tag):
if self.CurrentData == 'Test_Item':
print('Test_Item:', self.Test_Item)
elif self.CurrentData == 'Upper_Limit':
print('Upper_Limit:', self.Upper_Limit)
elif self.CurrentData == 'Lower_Limit':
print('Lower_Limit:', self.Lower_Limit)
elif self.CurrentData == 'Measure_Data':
print('Measure_Data:', self.Measure_Data)
elif self.CurrentData == 'Measure_Time':
print('Measure_Time:', self.Measure_Time)
elif self.CurrentData == 'Pass':
print('Pass:', self.Pass)
self.CurrentData = ''
#事件內容處理
def characters(self, content):
if self.CurrentData == 'Test_Item':
self.Test_Item = content
elif self.CurrentData == 'Upper_Limit':
self.Upper_Limit = content
elif self.CurrentData == 'Lower_Limit':
self.Lower_Limit = content
elif self.CurrentData == 'Measure_Data':
self.Measure_Data = content
elif self.CurrentData == 'Measure_Time':
self.Measure_Time = content
elif self.CurrentData == 'Pass':
self.Pass = content
if (__name__ == '__main__'):
#創建一個XMLReader
parser = xml.sax.make_parser()
# turn off namespaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
# 重写 ContextHandler
Handler = FireStormHandler()
parser.setContentHandler(Handler)
parser.parse("FIR205FMBP0001_14-32-43.xml")
報錯如下,該怎麼解決:Traceback (most recent call last):
File "D:\Python1\lib\xml\sax\expatreader.py", line 217, in feed
self._parser.Parse(data, isFinal)
xml.parsers.expat.ExpatError: junk after document element: line 11, column 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\21870405\Desktop\testXML.py", line 62, in <module>
parser.parse("FIR205FMBP0001_14-32-43.xml")
File "D:\Python1\lib\xml\sax\expatreader.py", line 111, in parse
xmlreader.IncrementalParser.parse(self, source)
File "D:\Python1\lib\xml\sax\xmlreader.py", line 125, in parse
self.feed(buffer)
File "D:\Python1\lib\xml\sax\expatreader.py", line 221, in feed
self._err_handler.fatalError(exc)
File "D:\Python1\lib\xml\sax\handler.py", line 38, in fatalError
raise exception
xml.sax._exceptions.SAXParseException: FIR205FMBP0001_14-32-43.xml:11:0: junk after document element
|