Fetching Daily Exchange Rates from xml feed provided by Turkish National Bank

May 29, 2017 Yalcin Kumbasar

I will present you a simple Jython script to fetch daily exchange rates using the xml feed provided by Turkish National Bank via the following url:










Figure 1 - Caption of Web Page for Exchange Rates

Let me show the partial page source which will present the xml structure clearly:



























Figure 2 - Caption of Source xml for Exchange Rates

Now, I want to show a screenshot for the Exchange Rates application of Maximo which also shows the result of our development.













Figure 3 - Exchange Rates Application of Maximo

In Figure 3, you can see exchange rates from EURO to Turkish Lira (TL or TRY) and US Dollar to Turkish Lira for today. One can compare these rates to the values provided by national bank and figure out that these values are Banknote Selling rates.



First, we add a system property for defining the URL provided from Turkish National Bank.










Figure 4 - System Properties Application showing the URL Property

Then, we create an Automation Script with action type of launch point. It works on ORGANIZATION object. The script loads the xml document from the url defined in property and then stores currency codes and banknote selling rates of each currency element as a pair of a python dictionary. Lastly, we select the currencies we want to save from the dictionary and add them to Maximo EXCHANGE table.


# Action to Fetch Exchange Rates and Save to Maximo 
from java.net import URL
from java.text import DateFormat, SimpleDateFormat
from java.util import Date, Properties
from javax.xml.parsers import DocumentBuilder, DocumentBuilderFactory
from org.w3c.dom import Document, Element, Node, NodeList
from psdi.mbo import Mbo, MboRemote, MboSet, MboSetRemote
from psdi.app.currency import Exchange, ExchangeRemote, ExchangeSet, ExchangeSetRemote
from psdi.server import MXServer
from sys import *

# function to load the xml document from URL
def loadXMLDocument():
    properties = MXServer.getMXServer().getConfig()
    property = properties.getProperty("tcmb.exchange.url")
    url = URL(property)
    factory = DocumentBuilderFactory.newInstance()
    return factory.newDocumentBuilder().parse(url.openStream())

# function to get exchange rates for all currencies provided
def getCurrencies(doc):
    currencies = {}
    nodes = doc.getElementsByTagName("Currency")
    cnt = nodes.getLength()
    print str(cnt)
    for i in range(cnt):
        element = nodes.item(i)
        code = element.getAttribute("CurrencyCode")
        subelement = element.getElementsByTagName("BanknoteSelling").item(0)
        rate = float(subelement.getTextContent()) if subelement.getFirstChild() != None else 0.0
        currencies[code] = rate
    return currencies

# function to save a currency and its exhange rate for Turkish Lira to Maximo
def setCurrency(code, rate):
    todayBegin = MXServer.getMXServer().getDate()
    todayEnd = MXServer.getMXServer().getDate()

    exchSet = mbo.getMboSet("EXCHANGE")
    exchange = exchSet.add()
    exchange.setValue("ORGID", "TEST", 11L)
    exchange.setValue("CURRENCYCODE", code, 11L)
    exchange.setValue("CURRENCYCODETO", "TL", 11L)
    exchange.setValue("EXCHANGERATE", rate, 11L)
    exchange.setValue("ACTIVEDATE", todayBegin, 11L)
    exchange.setValue("EXPIREDATE", todayEnd, 11L)

# Main - save exchange rates for USD and EUR to Turkish Lira 
doc = loadXMLDocument()
curr = getCurrencies(doc)
setCurrency("USD", curr['USD'])
setCurrency("EUR", curr['EUR'])

Code 1 - Jython Script for Action

Finally, the action for the code piece above is substituted in an escalation which runs automatically on daily basis in order to fetch the exchange rates and save them to Maximo database. Do not forget to tick "Repeat?" check box for the escalation point.















Figure 5 - Escalation for Exchange Rates Action in Maximo

All in all, this implementation is a practice of parsing an xml document provided via a url. One can alter the code piece due to their specific needs for other urls and currencies.

Previous Article
Maximo Rest Client Example: Calling an External REST Service (Logo Tiger 3) with a Maximo Client

MOTIVATION: When I searched for a REST client implemented within Maximo using Automation Scripting, all doc...

Next Article
MAXIMO IOT Newsletter: May Edition - Your Monthly updates - All in one place
MAXIMO IOT Newsletter: May Edition - Your Monthly updates - All in one place

      MAXIMO IoT Newsletter: May Edition - Your Monthly updates - All in one place   The IoT newsletter wil...


Want our latest news? Subscribe to our blog!

Last Name
First Name
Thank you!
Error - something went wrong!