0

I'm supposed to convert a small flat text (txt) file to XML for a program I'm running in Scala but don't know where to start. I've tried various methods but am having trouble so far. My file is of a few strings and several integers.

I'm still using very basic Scala, so the simpler, the better!

Thanks in advance to anyone who helps out.

  • 3
    We need a lot more information. What does your text file look like? What kind of XML do you want to produce? – Ray Toal May 07 '12 at 04:53
  • @RayToal is right. However I'd recommand you to have a look at Parser Combinator first, which is a way to create External DSL; after having slurped the text file for instance. For further usage, you'll potentially interrested by Iteratee which will give you more control while compiling from text to xml – Andy Petrella May 07 '12 at 06:05
  • @andypetrella for file with a few strings parser combinators could be an overkill – om-nom-nom May 07 '12 at 06:35

2 Answers2

4

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! :)

Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59
-3

I don't know what exactly you are trying to do. If you just wanted to wrap the values, you might do it similar to this:

def string2xml[A](a: A): String = a match {
  case s: String => "<String>" + s + "</String>"
  case i: Int => "<Int>" + i + "</Int>"
  case any => "<Any>" + any + "</Any>"
}
tgr
  • 3,557
  • 4
  • 33
  • 63