在Scala中编组/解组XML [英] Marshalling/unmarshalling XML in Scala

查看:104
本文介绍了在Scala中编组/解组XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究在Scala和XML之间封送/解组数据的各种方法,并且我对获得社区反馈(最好是基于第一手的知识/经验)感兴趣.

I am looking at various approaches for marshalling/unmarshalling data between Scala and XML, and I'm interested in getting community feedback (preferably grounded in first-hand knowledge/experience).

我们当前正在使用JAXB,这很好,但是我希望有一个纯粹的Scala解决方案.我正在考虑以下方法:

We're currently using JAXB, which is fine, but I'm hoping for a pure Scala solution. I'm considering the following approaches:

  1. 使用Scala的内置XML工具:Scala-> XML会很容易,但是我猜想另一个方向会很痛苦.另一方面,这种方法支持任意翻译逻辑.

  1. Use Scala's built-in XML facilities: Scala->XML would be easy, but my guess is that the other direction would be fairly painful. On the other hand, this approach supports arbitrary translation logic.

数据绑定: scalaxb 目前似乎还不成熟,并且无法处理我们当前的架构,并且我不知道任何其他用于Scala的数据绑定库.像JAXB一样,需要额外的翻译层来支持涉及的转换.

Data binding: scalaxb seems to be somewhat immature at the moment and doesn't handle our current schema, and I don't know of any other data binding library for Scala. Like JAXB, an extra translation layer is required to support involved transformations.

XML Pickler组合器: GData Scala Client 库提供了XML Pickler组合器,但是最近的项目活动很少,我不知道当前状态是什么.

XML pickler combinators: The GData Scala Client library provides XML pickler combinators, but recent project activity has been low and I don't know what the current status is.

问题:

  1. 您对我列出的方法/库有何经验?
  2. 两者的相对优缺点是什么?
  3. 还有其他我应该考虑的方法或Scala库吗?

我在自己对这个问题的回答中添加了一些关于我对pickler组合器的早期印象的注释,但是我仍然对真正了解各种方法的人的反馈非常感兴趣.我希望可以进行比较全面的比较,以帮助开发人员选择适合他们需求的方法.

I added some notes on my early impressions of pickler combinators in my own answer to this question, but I'm still very interested in feedback from someone who actually knows the various approaches in depth. What I'm hoping for is a somewhat comprehensive comparison that would help developers choose the right approach for their needs.

推荐答案

我建议使用Scala的内置XML功能.我刚刚为看起来像这样的文档结构实现了反序列化:

I recommend using Scala's built-in XML features. I've just implemented deserialization for a document structure that looks like this:

val bodyXML = <body><segment uri="foo"><segment uri="bar" /></segment></body>

请注意,各段可以相互嵌套.

Note that the segments can be nested within each other.

细分的实现方式如下:

case class Segment(uri: String, children: Seq[Segment])

要反序列化XML,请执行以下操作:

To deserialize the XML, you do this:

val mySegments = topLevelSegments(bodyXML)

...而topLevelSegments的实现只是几行代码.请注意递归,它涉及XML结构:

...and the implementation of topLevelSegments is just a few lines of code. Note the recursion, which digs through the XML structure:

def topLevelSegments(bodyXML: Node): Seq[Segment] = 
    (bodyXML \ "segment") map { nodeToSegment }

def nodeToSegment = (n: Node) => Segment((n \ "@uri")(0) text, childrenOf(n))

def childrenOf(n: Node): Seq[Segment] = (n \ "segment") map { nodeToSegment }

希望有帮助.

这篇关于在Scala中编组/解组XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆