【问题标题】:How to put a byte array into XML in Scala如何在Scala中将字节数组放入XML
【发布时间】:2012-03-16 14:50:29
【问题描述】:

我正在与 .Net Web 服务进行交互。根据服务描述,服务器需要一个 base64Binary 类型。

这就是我尝试构建 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>

在上面的 sn-p 中,request.getContent().array() 是我从 PhoneGap 中开发的移动应用程序接收到的 HTTP 请求。

服务器响应 FileContents 无效。有什么想法吗?

【问题讨论】:

    标签: scala cordova finagle


    【解决方案1】:

    您当前的版本只是将字节(我假设 request.getContent().array() 是一个字节数组)写入以空格分隔的 base-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>
    

    您可能需要稍微修改一下选项,但这更有可能是您需要的。

    【讨论】:

    • 我太傻了(为我辩护——那是星期五 ;-)。感谢您的解决方案。你是救生员,特拉维斯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2021-11-23
    • 2011-02-10
    • 2018-12-26
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多