【问题标题】:Using play's wsclient to make a call to fetch a RSS feed and return a collection使用 play 的 wsclient 进行调用以获取 RSS 提要并返回集合
【发布时间】:2017-06-23 18:46:12
【问题描述】:

我正在向 URL 发出请求以获取 RSS xml 数据:

 val items: List[Item] = ws.url("http://www.example.com/rss.xml").get().map { response =>

 }

现在我想遍历所有节点并创建一个项目集合。

我的物品箱类是:

 case class Item(title: String, link: String, description: String, guid: String)

XML 格式如下:

 <rss ...>
   <channel>
      <title>
      </title>
      <link />
      <description />
      <item>
        <title>hello</title>
        <description> ... </description>
        <guid> ... </guid>
      </item>
      <item>
      ...
      </item>
    </channel>
 </rss>

我不确定如何处理调用 .map 以遍历项目的响应以及如何返回项目案例类的集合。

【问题讨论】:

标签: scala playframework


【解决方案1】:

您基本上应该使用 xml api 来提取相关的NodeSeq,它的工作原理与任何集合一样。 然后映射每个节点以提取相关的子标签内容并从中创建一个对象。

val itemNodes = response.xml \\ "item" //will get all item nodes as a NodeSeq
val item = itemNodes.map(node => 
        Item(
          (node \ "title").text,
          "", // there's no link tag in the item
          (node \ "description").text,
          (node \ "guid").text
        )
      )

如果不完全是这样,类似的东西

【讨论】:

    猜你喜欢
    • 2013-02-22
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    相关资源
    最近更新 更多