Friday, April 25, 2008

Oh how I miss these days!!

I sometimes miss the days of being in the Military (Semper Fi), and being in the desert with nothing to do, here is a nice little video of what happens when bordem sets in.

Monday, April 21, 2008

Groovy XMLSlurper

If you know anything about Groovy, I'm sure you have seen the XML parsing features it brings to the table. Whether you have seen it or not it is definitely worth mentioning. I feel this is a great tool added to my Java tool chest.

Imagine you have the following XML:

<basketball team="Kansas" status="National Champions">
<player>
<name>Brandon Rush</name>
<position>Small Forward</position>
<pts-per-game>13.5</pts-per-game>
<rebs-per-game>6.2</rebs-per-game>
</player>
<player>
<name>Mario Chalmers</name>
<position>Guard</position>
<pts-per-game>12.5</pts-per-game>
<rebs-per-game>4.2</rebs-per-game>
</player>
<player>
<name>Sherron Collins</name>
<position>Guard</position>
<pts-per-game>10.5</pts-per-game>
<rebs-per-game>3.4</rebs-per-game>
</player>
<player>
<name>Darrel Arthur</name>
<position>Power Forward</position>
<pts-per-game>14.2</pts-per-game>
<rebs-per-game>6.9</rebs-per-game>
</player>
<player>
<name>Darnell Jackson</name>
<position>Power Forward</position>
<pts-per-game>12.5</pts-per-game>
<rebs-per-game>8.9</rebs-per-game>
</player>
</basketball>


Now that you have your XML you want to parse it and put it into a Player object. Let's go ahead and create our Player object:


class Player {
String name
String position
double ptsPerGame
double rebsPerGame

def String toString() {
return "Name: ${name}\r\nPosition: ${position}\r\nPoints Per Game: ${ptsPerGame}\r\nRebs Per Game${rebsPerGame}"
}
}

Now that we have our Player class created, lets create a class that will consume the XML create a Player for each Player in the XML:


class ParsePlayers {
private List players

ParsePlayers () {
players = new ArrayList()
}

def Player[] parsePlayers() {
def data = this.getClass().getResource("/players.xml").text
def xmlSlurper = new XmlSlurper()
def playersXml = xmlSlurper.parseText(data)

playersXml.player.each {
def player = new Player()

player.name = it.name.text()
player.position = it.position.text()
player.ptsPerGame = Double.parseDouble(it.'pts-per-game'.text().trim())
player.rebsPerGame = Double.parseDouble(it.'rebs-per-game'.text().trim())

players.add(player)
}
return (Player[])players.toArray()
}
}


So, as you can see above that is a pretty simple way to parse XML using XMLSlurper from Groovy. I don't know about you, but that makes working with XML a little bit nicer.