Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.67 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how to get the data from xml feeds
  2. //
  3. // Dependencies
  4. // ============
  5. import groovy.sql.Sql
  6.  
  7. @Grapes([
  8.     @Grab(group='com.h2database', module='h2', version='1.3.163'),
  9.     @GrabConfig(systemClassLoader=true)
  10. ])
  11.  
  12. //
  13. // Main program
  14. // ============
  15. def sql = Sql.newInstance("jdbc:h2:db/cricket", "user", "pass", "org.h2.Driver")
  16.  
  17. def dataUrl = new URL("http://scores.cricandcric.com/cricket/getFeed?key=4333433434343&format=xml&tagsformat=long&type=schedule")
  18.  
  19. dataUrl.withReader { reader ->
  20.     def feeds = new XmlSlurper().parse(reader)
  21.  
  22.     feeds.matches.match.each {
  23.         def data = [
  24.             it.id,
  25.             it.name,
  26.             it.type,
  27.             it.tournamentId,
  28.             it.location,
  29.             it.date,
  30.             it.GMTTime,
  31.             it.localTime,
  32.             it.description,
  33.             it.team1,
  34.             it.team2,
  35.             it.teamId1,
  36.             it.teamId2,
  37.             it.tournamentName,
  38.             it.logo
  39.         ].collect {
  40.             it.text()
  41.         }
  42.  
  43.         sql.execute("INSERT INTO matches (id,name,type,tournamentId,location,date,GMTTime,localTime,description,team1,team2,teamId1,teamId2,tournamentName,logo) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", data)
  44.     }
  45. }
  46.        
  47. <id>1263</id>
  48. <name>Australia v India 3rd Test at  Perth - Jan 13-17, 2012</name>
  49. <type>TestMatch</type>
  50. <tournamentId>137</tournamentId>
  51. <location>Perth</location>
  52. <date>2012-01-14</date>
  53. <GMTTime>02:30:00</GMTTime>
  54. <localTime>10:30:00</localTime>
  55. <description>3rd Test day 2</description>
  56. <team1>Australia</team1>
  57. <team2>India</team2>
  58. <teamId1>7</teamId1>
  59. <teamId2>1</teamId2>
  60. <tournamentName>India tour of Australia 2011-12</tournamentName>
  61. <logo>/cricket/137/tournament.png</logo>