Read XML file while it is being written (in Python)

前端 未结 2 1287
-上瘾入骨i
-上瘾入骨i 2021-01-25 10:06

I have to monitor an XML file being written by a tool running all the day. But the XML file is properly completed and closed only at the end of the day.

Same constraint

2条回答
  •  终归单人心
    2021-01-25 10:27

    Three hours after posting my question, no answer received. But I have finally implemented the simple example I was looking for.

    My inspiration is from saaj's answer and is based on xml.sax and watchdog.

    from __future__ import print_function, division
    import time
    import watchdog.events
    import watchdog.observers
    import xml.sax
    
    class XmlStreamHandler(xml.sax.handler.ContentHandler):
      def startElement(self, tag, attributes):
        print(tag, 'attributes=', attributes.items())
        self.tag = tag
      def characters(self, content):
        print(self.tag, 'content=', content)
    
    class XmlFileEventHandler(watchdog.events.PatternMatchingEventHandler):
      def __init__(self):
        watchdog.events.PatternMatchingEventHandler.__init__(self, patterns=['*.xml'])
        self.file = None
        self.parser = xml.sax.make_parser()
        self.parser.setContentHandler(XmlStreamHandler())
      def on_modified(self, event):
        if not self.file:
          self.file = open(event.src_path)
        self.parser.feed(self.file.read())
    
    if __name__ == '__main__':
      observer = watchdog.observers.Observer()
      event_handler = XmlFileEventHandler()
      observer.schedule(event_handler, path='.')
      try:
        observer.start()
        while True:
          time.sleep(10)
      finally:
        observer.stop()
        observer.join()
    

    While the script is running, do not forget to touch one XML file, or simulate the on-the-fly writing using the following command:

    while read line; do echo $line; sleep 1; done out.xml &
    

提交回复
热议问题