功能xml在scala中构建

时间:2011-03-09 05:02:10

标签: xml scala functional-programming

采用任意文本格式的文档的最佳方法是什么,例如:

Hello world

1.1.1 (John Doe)

Paragraph one here... pargraph one continued here

paragraph two here

并将其转换为XML文档,如下所示:

<note id="1.1.1" author_first = "John" author_last = "Doe">
  <paragraph>Paragraph one here... paragraph one continued here</paragraph>
  <paragraph>paragraph two here</paragraph>
</note>

我唯一能想到的是使用XMLBuilder类型的标准命令式方法是Scanner。但这似乎不像惯用的Scala。

谢谢!

1 个答案:

答案 0 :(得分:1)

假设您已经解析了数据,那么您可以采用文字方法......

val paragraphs: List[String] = ...
val noteId: String = ...
val authorName: (String, String) = ...

<note id={noteId} author_first={authorName._1} author_last={authorName._2}>
  {paragraphs.map(s => <paragraph>{s}</paragraph>)}
</note>