Created
June 17, 2013 18:33
-
-
Save worthlesscog/5799098 to your computer and use it in GitHub Desktop.
CSV to XML converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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