gmhk
gmhk

Reputation: 15960

how to get the data from xml feeds

I have the following feeds from my vendor, http://scores.cricandcric.com/cricket/getFeed?key=4333433434343&format=xml&tagsformat=long&type=schedule

I wanted to get the data from that xml files as java objects, so that I can insert into my database regularly. The above data is nothing but regular updates from the vendor, so that I can update in my website.

can you please suggest me what are my options available to get this working

Should I use any webservices or just Xstream to get my final output.. please suggest me as am a new comer to this concept

Vendor has suggested me that he can give me the data in following 3 formats rss, xml or json, I am not sure what is easy and less consumable to get it working

Upvotes: 1

Views: 316

Answers (3)

Mark O'Connor
Mark O'Connor

Reputation: 77991

I would suggest just write a program that parses the XML and inserts the data directly into your database.

Example

This groovy script inserts data into a H2 database.

// 
// Dependencies
// ============
import groovy.sql.Sql

@Grapes([
    @Grab(group='com.h2database', module='h2', version='1.3.163'),
    @GrabConfig(systemClassLoader=true)
])

//
// Main program
// ============
def sql = Sql.newInstance("jdbc:h2:db/cricket", "user", "pass", "org.h2.Driver") 

def dataUrl = new URL("http://scores.cricandcric.com/cricket/getFeed?key=4333433434343&format=xml&tagsformat=long&type=schedule")

dataUrl.withReader { reader ->
    def feeds = new XmlSlurper().parse(reader)

    feeds.matches.match.each {
        def data = [
            it.id,
            it.name,
            it.type,
            it.tournamentId,
            it.location,
            it.date,
            it.GMTTime,
            it.localTime,
            it.description,
            it.team1,
            it.team2,
            it.teamId1,
            it.teamId2,
            it.tournamentName,
            it.logo
        ].collect {
            it.text()
        }

        sql.execute("INSERT INTO matches (id,name,type,tournamentId,location,date,GMTTime,localTime,description,team1,team2,teamId1,teamId2,tournamentName,logo) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", data)
    }
}

Upvotes: 1

Mohammad Dashti
Mohammad Dashti

Reputation: 755

Actually you have RESTful store that can return data in several formats and you only need to read from this source and no further interaction is needed. So, you can use any XML Parser to parse XML data and put the extracted data in whatever data structure that you want or you have.

I did not hear about XTREME, but you can find more information about selecting the best parser for your situation at this StackOverflow question.

Upvotes: 0

craigmj
craigmj

Reputation: 5077

Well... you could use an XML Parser (stream or DOM), or a JSON parser (again stream of 'DOM'), and build the objects on the fly. But with this data - which seems to consist of records of cricket matches, why not go with a csv format?

This seems to be your basic 'datum':

<id>1263</id>
<name>Australia v India 3rd Test at  Perth - Jan 13-17, 2012</name>
<type>TestMatch</type>
<tournamentId>137</tournamentId>
<location>Perth</location>
<date>2012-01-14</date>
<GMTTime>02:30:00</GMTTime>
<localTime>10:30:00</localTime>
<description>3rd Test day 2</description>
<team1>Australia</team1>
<team2>India</team2>
<teamId1>7</teamId1>
<teamId2>1</teamId2>
<tournamentName>India tour of Australia 2011-12</tournamentName>
<logo>/cricket/137/tournament.png</logo>

Of course you would still have to parse a csv, and deal with character delimiting (such as when you have a ' or a " in a string), but it will reduce your network traffic quite substantially, and likely parse much faster on the client. Of course, this depends on what your client is.

Upvotes: 0

Related Questions