2015-11-17 53 views
1

我有一個JSON文件是這樣的:轉換JSON爲XML命名空間和令牌斯卡拉

{"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))

我使用的情況下,類出貨創建對象並將其傳遞給:

val xmlIB = xstreamIB.toXML(Shipment)

輸出XML文件返回:

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

接收API要求2米的東西。打開貨件標籤中的名稱空間; <Shipment xmlns="namespace">,以及地址標記中的一個標記; <Address type = "shipping">。我嘗試使用.alias,但它修改了打開和關閉標記;拋出一個錯誤。

有沒有辦法將名稱空間和令牌添加到開始標記?

回答

0

我能夠使用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)