2012-03-16 71 views
1

我正在與.Net Web服務交互。根據服務描述,服務器需要base64Binary類型。如何在Scala中將字節數組放入XML中

這是如何我試圖建立SOAP分組:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Header> 
    </soap:Header> 
    <soap:Body> 
     <uploadFile xmlns="http://localhost/"> 
     <FileDetails> 
      <ReferenceNumber>123</ReferenceNumber> 
      <FileName>testfile</FileName> 
      <FullFilePath>file</FullFilePath> 
      <FileType>1</FileType> 
      <FileContents>{request.getContent().array()}</FileContents> 
     </FileDetails> 
     </uploadFile> 
     </soap:Body> 
    </soap:Envelope> 

request.getContent().array()上面的代碼段我是從在PhoneGap的開發的移動應用程序接收的HTTP請求。

服務器響應FileContents無效。有任何想法嗎?

回答

1

您當前的版本是剛寫入的字節(我假設request.getContent().array()是字節數組)的空間分隔基10的整數:

scala> val bytes = 1 to 10 map(_.toByte) toArray 
bytes: Array[Byte] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 

scala> <FileContents>{bytes}</FileContents> 
res0: scala.xml.Elem = <FileContents>1 2 3 4 5 6 7 8 9 10</FileContents> 

這絕對不是你想要的。您可以使用庫像Apache Commons Codec字節數組編碼爲一個字符串(在這裏我使用了Base64 encoder):

scala> import org.apache.commons.codec.binary.Base64 
import org.apache.commons.codec.binary.Base64 

scala> <FileContents>{Base64.encodeBase64String(bytes)}</FileContents> 
res1: scala.xml.Elem = <FileContents>AQIDBAUGBwgJCg==</FileContents> 

你可能有選項鼓搗了一下,但這更可能成爲你需要的東西。

+0

這真是愚蠢的我(在我的防守 - 這是星期五;-)。感謝您的解決方案。你是救生員,特拉維斯。 – Jack 2012-03-19 07:45:55