scala 从内部数据结构到xml的转化,目前要用的只有xml字面量和转义符
例如:
/**
* xml 序列化
*/
abstract class CCTherm {
val description: String
val yearMade: Int
val dateObtained: String
val bookPrice: Int
val purchasePrice: Int
val condition: Int
override def toString = description
def toXML =
<cctherm>
<description>{description}</description>
<yearMade>{yearMade}</yearMade>
<dateObtained>{dateObtained}</dateObtained>
<bookPrice>{bookPrice}</bookPrice>
<purchasePrice>{purchasePrice}</purchasePrice>
<condition>{condition}</condition>
</cctherm>
}
object xmlTest {
def main(args: Array[String]): Unit = {
val therm = new CCTherm {
val description = "hot dog #5"
val yearMade = 1952
val dateObtained = "March 14,2014"
val bookPrice = 2199
val purchasePrice = 500
val condition = 9
}
println(therm.toXML)
}
}
运行的结果
<cctherm>
<description>hot dog #5</description>
<yearMade>1952</yearMade>
<dateObtained>March 14,2014</dateObtained>
<bookPrice>2199</bookPrice>
<purchasePrice>500</purchasePrice>
<condition>9</condition>
</cctherm>