-1

I am a newbie to Python. I have an XML file in the server which will be updated by a device with notifications periodically like below.

<notifications
    xmlns="http://XXXXX">
    <notification>
        <received>2016-09-04T13:48:57Z</received>
        <source-address>x.x.x.x</source-address>
        <notification-type>type-1</notification-type>
        <message>message-1</message>
    </notification>
    <notification>
        <received>2016-09-04T13:49:21Z</received>
        <source-address>X.X.X.X</source-address>
        <notification-type>type-2</notification-type>
        <message>message-2</message>
    </notification>
    <notification>
        <received>2016-09-04T13:52:38Z</received>
        <source-address>X.X.X.X</source-address>
        <notification-type>type-3</notification-type>
        <message>message-3</message>
    </notification>
    <notification>
        <received>2016-09-04T14:01:21Z</received>
        <source-address>X.X.X.X</source-address>
        <notification-type>type-4</notification-type>
        <message>message-4</message>
    </notification>
    <notification>
        <received>2016-09-04T14:01:51Z</received>
        <source-address>X.X.X.X</source-address>
        <notification-type>type-5</notification-type>
        <message>message-5</message>
    </notification>
</notifications>

I am interested in getting the values of <notification-type> and <message> whenever device post notification on to this file. Is there a way I can do this in Python?

1 Answers1

0

two separate issues – first issue is monitoring a file for changes … this looks like it’s been addressed

second is reading the XML file in Python

import xml.etree.ElementTree as Et

def process(notification):

    received          = notification[0].text
    print (received)

    notification_type = notification[2].text
    print (notification_type)


xml = Et.ElementTree()
xml.parse('notifications.xml')
root = xml.getroot()

for notification in root:
    process(notification)
Community
  • 1
  • 1
jross
  • 1,129
  • 9
  • 10