小编vic*_*ont的帖子

Play框架:将XML解析为模型

我在Play框架驱动的webapp中有简单的实体.它看起来像这样:

case class MyItem(id: Option[Long] = None, name: String, comments: List[Comment])
case class Comment(commentDate: Date, commentText: String)
Run Code Online (Sandbox Code Playgroud)

我从DB获取XML,如下所示:

<?xml version="1.0"?>
<item>
    <id>1</id>
    <name>real item</name>
    <comments> 
        <comment>
            <comment_date>01.01.1970</comment_date>
            <comment_text>it rocks</comment_text>
        </comment>
        <comment>
            <comment_date>02.01.1970</comment_date>
            <comment_text>it's terrible</comment_text>
        </comment>      
    </comments>
</item>
Run Code Online (Sandbox Code Playgroud)

现在我不知道将它解析为模型和表单映射.

我的表单映射以防万一(现在不编译):

  val itemForm = Form(
    mapping(
      "id" -> optional(longNumber),
      "name" -> nonEmptyText,
      "comments" -> list(mapping(
          "commentDate" -> date("dd.mm.yyyy"),
          "commentText" -> text
      )(Comment.apply)(Comment.unapply))
    )(MyItem.apply)(MyItem.unapply)
  )
Run Code Online (Sandbox Code Playgroud)

scala xml-parsing playframework-2.0

6
推荐指数
1
解决办法
2261
查看次数

标签 统计

playframework-2.0 ×1

scala ×1

xml-parsing ×1