There are several steps to this.
First, I'd determine the format-independent structure of the data and create a Scala class to hold this data. Then I'd figure out how this data is formatted in the text file, and write a routine to parse the text file and populate the class (or possibly a list of that class) with the data. And finally, figure out the schema for the XML file and write the data to it using Scala's XML literals.
Suppose your data consists of a person's name, age, height, and weight. A Scala class to hold that data could look like:
case class Person(name: String, age: Int, height: Double, weight: Double)
In a text file, this could be represented in several ways, from the simplest:
Peter
34
178.2
83.2
or a "properties" style format:
name = Peter
age = 34
weight = 178.2
height = 83.2
or the most likely CSV-format, if you have several persons in one file:
Peter,34,178.2,83.2
Tom,53,182.3,95.3
John,27,175.1,74.5
Let's take the last format as an example. The simplest way to read the file is like this:
val lines = io.Source.fromFile(new java.io.File("test.txt")).getLines.toList
Then parse each line into a Person:
val persons = lines map { line =>
val elements = line split ","
Person(elements(0), elements(1).toInt, elements(2).toDouble, elements(3).toDouble)
}
Add a nice method on the Person class to turn it into some decent XML:
def toXml =
<person>
<name>{ name }</name>
<age>{ age }</age>
<height>{ height }</height>
<weight>{ weight }</weight>
</person>
And finally map the list of persons to XML:
val personsXml = <persons>{ persons map(_.toXml) }</persons>
And write it to a file:
new java.io.FileOutputStream(new java.io.File("test.xml")).write(personsXml.toString.getBytes)
Note that error-handling and proper closing of files is left as an exercise for the reader! :)