在 Scala 中使用命名空间和标记将 json 转换为 XML

Convert json to XML with namespace and tokens in Scala

我有一个这样的 json 文档:

{"orderNumber": "12345",
"date": "11/05/2011",
"fromInventoryLocation": "New York",
"toLocation": "Los Angeles",
"shippingMethod": "Ground",
"shipDate": "11/25/2014",
"shipTo": "123 Main St.",
"Items": [
   {"item": "shirt", "quantity": "2", "orderPriority": "Standard"}, 
]}

我初始化 XStream 转换器:

val xstreamIB = xstream.XStreamConversions(new XStream(new DomDriver))

我用case class Shipment创建对象并传递给:

val xmlIB = xstreamIB.toXML(Shipment)

输出XML文件returns:

<Shipment>
  <OrderNumber>12345</OrderNumber>
  <Date>11/05/2011</Date>
  <Address>
    <Street>123 Main St.</Street>
  </Address>
  <Etc>
    <Ex>...</Ex>
  </Etc>
</Shipment

接收API需要两件事。起始 Shipment 标签中的命名空间; <Shipment xmlns="namespace">,以及地址标签中的token; <Address type = "shipping">。我尝试使用 .alias,但它同时修改了开始和结束标记;抛出错误。

有没有办法将命名空间和令牌添加到开始标记中?

我能够使用 StaxDriver 而不是 DomDriver 获取 xml 命名空间。以下代码有效:

val qmap = new QNameMap
  qmap.setDefaultNamespace("urn:namespace")
  val xmlOut = xstream.XStreamConversions(new XStream(new StaxDriver(qmap)))
val xml = xmlOut.toXML(Shipment)