I’m writing an application where I would like to log calls to methods, by creating an object for each run of the method call, where the object’s class has a field for input arguments and another field for the return value.
I’ve seen in Chapter 28 of Staircase Book, “Working with XML”, a way of writing a single xml node to a file using scala.xml.XML.save
.
However, my program runs at different times so I would like to log the data over time.
When I want to review the data, I would like to see it in an HTML table, where each <td>
corresponds to one element in the serialized data (where element of the XML maps to one field in the case class).
Is there a best and simple way of doing this? The data will always be well-formed and just used internally so I don’t want to overcomplicate it.
Also, is this going to be possible, because appending to an XML file might be problematic because it needs to have a closed tag? eg
<catalog> <functioncall> <time>xx</time> <input>xx</input> <result>xx</result> </functioncall> <functioncall> <time>xx</time> <input>xx</input> <result>xx</result> </functioncall> ...
Edit:
Have found this in the meantime
val catalog = scala.xml.XML.loadFile(myFile.getAbsolutePath)
import scala.xml._
def addChild(n: Node, newChild: Node) = n match {
case Elem(prefix, label, attribs, scope, child @ _*) =>
Elem(prefix, label, attribs, scope, true, child ++ newChild : _*)
case _ => throw new Exception("Can only add children to elements!")
}
addChild(catalog, this.toXML)
scala.xml.XML.save(myFile.getAbsolutePath, catalog)
but not sure if the best way.