Skip to content

Instantly share code, notes, and snippets.

@worthlesscog
Created June 17, 2013 18:33
Show Gist options
  • Save worthlesscog/5799098 to your computer and use it in GitHub Desktop.
Save worthlesscog/5799098 to your computer and use it in GitHub Desktop.
CSV to XML converter
import scala.io.Source
case class Passenger(name: String, ridingIn: String) {
def asNode = <Passenger name={ name }/>
}
case class Car(model: String, year: String, registration: String, passengers: Seq[Passenger]) {
def asNode =
<Car model={ model } year={ year } reg={ registration }>
<Passengers>
{ passengers map (_.asNode) }
</Passengers>
</Car>
override def toString = asNode toString
}
object SimpleXml {
def csv(file: String) = Source.fromFile(file).getLines.map(_.split(",")).toSeq
def main(args: Array[String]): Unit = {
val ps = csv("passengers.csv").map(p ⇒ Passenger(p(1), p(0)))
val cs = csv("cars.csv").map(c ⇒ Car(c(1), c(2), c(0), ps.filter(_.ridingIn == c(0))))
cs foreach println
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment