Advertisement
Guest User

XMLReader

a guest
May 17th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.00 KB | None | 0 0
  1. package il.ac.hit
  2.  
  3. import java.io._
  4. import java.net.URL
  5. import java.text.SimpleDateFormat
  6. import java.util.StringTokenizer
  7. import scala.io.Source
  8. import scala.xml.XML
  9. import java.util.Date
  10.  
  11.  
  12. // the Model
  13. object XMLreader extends XMLReaderInterface{
  14.  
  15.  
  16.   def connectToUrl (): Int = {
  17.  
  18.     val url = new URL("http://www.abelski.com/currencies.xml")
  19.     //val url = new URL("http://www.boi.org.il/currency.xml")
  20.     val connection = url.openConnection()
  21.     val document = XML.load(connection.getInputStream)
  22.     var countCurrencies : Int = 0
  23.     val writer = new PrintWriter(new File("CurrenicesData.txt"))
  24.  
  25.       try{
  26.  
  27.         writer.write( ((document\\"CURRENCIES")\\"LAST_UPDATE").text )
  28.         writer.write(System.lineSeparator())
  29.  
  30.         for ( currentData <- (document\\"CURRENCY") ){
  31.           countCurrencies = countCurrencies + 1
  32.           writer.write( (currentData\\"CURRENCYCODE").text + " " +  (currentData\\"RATE").text )
  33.           writer.write(System.lineSeparator())
  34.         }
  35.  
  36.       }
  37.       finally writer.close()
  38.       countCurrencies
  39.   }
  40.  
  41.  
  42.   def updateData(currenciesNamesArray: Array[String], currenciesRatesArray: Array[Double]): java.util.Date ={
  43.  
  44.     connectToUrl()
  45.  
  46.     var ob = Source.fromFile("CurrenicesData.txt");
  47.     var i_local = 0
  48.  
  49.  
  50.       for( line <- ob.getLines.drop(1) )   // to not contain the first line of the file
  51.       {
  52.           var st = new StringTokenizer(line," ", false)
  53.           var currencyName = st.nextToken()
  54.           var currencyRate = st.nextToken().toDouble
  55.  
  56.             currenciesNamesArray(i_local) = currencyName
  57.             currenciesRatesArray(i_local) = currencyRate
  58.             i_local = i_local + 1
  59.       }
  60.  
  61.  
  62.       ob = Source.fromFile("CurrenicesData.txt");
  63.       val iterator = ob.getLines()
  64.       val stringDate = iterator.next()
  65.       val  simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  66.       val date = simpleDateFormat.parse(stringDate);
  67.  
  68.       date
  69.   } // end of method
  70.  
  71. }// end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement