qiangqiang1 发表于 2020-5-21 11:32:28

新做XML解析失敗報錯xml.sax._exceptions,看不懂

本帖最后由 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

Twilight6 发表于 2020-5-21 12:26:11

经过我精密的分析{:10_300:}
............
..........
........
.....
...
..
.
这帖会沉{:10_267:}

qiangqiang1 发表于 2020-5-21 13:27:26

<TESTITEM>
<Band />
<Channel>1</Channel>
<Test_Item>PRESSURE_FIRST</Test_Item>
<Upper_Limit>-294</Upper_Limit>
<Lower_Limit>-324</Lower_Limit>
<Measure_Data>-300</Measure_Data>
<Measure_Time>2020-05-19 14:32:43</Measure_Time>
<Pass>0</Pass>
</TESTITEM>
<TESTITEM>
<Band />
<Channel>2</Channel>
<Test_Item>PRESSURE_SECOND</Test_Item>
<Upper_Limit>-140</Upper_Limit>
<Lower_Limit>-155</Lower_Limit>
<Measure_Data>-150</Measure_Data>
<Measure_Time>2020-05-19 14:32:43</Measure_Time>
<Pass>0</Pass>
</TESTITEM>
</TestStation>
</root>
分享一下XML檔

qiangqiang1 发表于 2020-5-21 13:28:14

Twilight6 发表于 2020-5-21 12:26
经过我精密的分析
............
..........


页: [1]
查看完整版本: 新做XML解析失敗報錯xml.sax._exceptions,看不懂