Converting an xml file to byte array in ruby

Hi,

Can someone help me with converting an xml file to byte array in ruby ?
I have a requirement to send a file as a byte array to a SOAP based web
service.

I have created ruby methods to do some other functions which deal with
strings and arrays. But I am unable to convert file to byte array to use
it with a particular method.

Any help will be appreciated.
Thanks in advance.

Kind regards,
kazi

On Mon, May 27, 2013 at 5:22 PM, Khudadad K. [email protected]
wrote:

Can someone help me with converting an xml file to byte array in ruby ?
I have a requirement to send a file as a byte array to a SOAP based web
service.

That sounds so wrong. I mean, you have XML and a protocol which uses
XML
messages - and it’s especially tailored to allow for embedding arbitrary
XML content in the message body. Why send as a “byte array”? What is
this
supposed to mean anyway?

I have created ruby methods to do some other functions which deal with
strings and arrays. But I am unable to convert file to byte array to use
it with a particular method.

The thing which comes closes to a “byte array” in Ruby is a String with
encoding “BINARY”.

Cheers

robert

OK. to be more specific, here is my method:
def
add_file_to_project(ticket,pid,fileName,fileData,srcId,targetIds,metadata=nil)
require ‘savon’
[email protected]
client = Savon.client do
wsdl received_path
end
message = {‘Ticket’ => ticket, ‘ProjectID’ => pid,‘Filename’ =>
fileName, ‘FileData’=> fileData,‘SourceLanguageID’ => srcId,
‘TargetLanguageIDs’ => targetIds, ‘Metadata’ => nil,
‘IsReferenceMaterial’ => false, ‘WebServiceType’ => ‘Publisher’}
response = client.call(:add_file_to_project, :message => message)
addFileToProjectResult =
response.body[:add_file_to_project_response][:add_file_to_project_result]
return addFileToProjectResult
end

where in fileData should be a base64Binary (to which i referred as a
“byte array”).
Now, rest of the parameters are Strings except for ‘TargetLanguageIDs’
which is an array.
I just need to send a file in that fileData parameter. Please show how
to achieve that when I do not put restrictions on any kind of files.

On Mon, May 27, 2013 at 6:50 PM, Khudadad K. [email protected]
wrote:

fileName, ‘FileData’=> fileData,‘SourceLanguageID’ => srcId,

Well, but then it’s a string, isn’t it? Or what type is fileData?
(Btw.
in Ruby we usually use “file_data”).

I just need to send a file in that fileData parameter. Please show how

to achieve that when I do not put restrictions on any kind of files.

If it is a String already, just leave it as is. If not,
use Base64.encode64 to make it a String.

Kind regards

robert

Thanks for the help Robert. I am really trying hard to understand the
problem and to make it clear in my query as well.

I used the above information about Base64 provided by you.
file_data=Base64.encode64(File.read(“file_path”))
but I get a SOAP response (status 200) along with the request parameters
which I
provided.

Robert K. wrote in post #1110295:

Well, but then it’s a string, isn’t it? Or what type is fileData?
(Btw.
in Ruby we usually use “file_data”).

The WebService defines file_data parameter to be base64Binary, and I
have the equivalent code in java as follows:
FileInputStream fis = new FileInputStream(fileList[i]);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
for (int readNum; (readNum = fis.read(buff)) != -1;)
{
bos.write(buff, 0, readNum);
}
byte[] blob = bos.toByteArray();

I am investigating why I am getting a status 200 error.

Thanks again for your time. I will post my findings as I work my way
forward.

I have now tried various combinations. After I call the web services
method, I have a tool to go and check if the file was attached or
not.And none of the methods seem to work.

I have attached a whole stack trace for your reference.

Here is a summary of the various ways I have tried :
Step 1. Read the xml file
xmlfeed = Nokogiri::XML(open(“D:\FSII
AudioCo.NewMediaPlayerOffersVideo.xml”))

Step 2. convert to Base64binary and call (Please refer my previous post
to see the method that I am calling)
trial 1. fileData=Base64.encode64(xmlfeed)
Result: SOAP response (status 200) with a “=>nil” at the end.

  trial 2. fileData=xmlfeed.to_s converting to string as it is.
  Result: Savon::HTTPError: HTTP error (400): Bad Request

   trial 3. fileData=Base64.encode64(xmlfeed.to_s)
  Result: SOAP response (status 200) with a "=>nil" at the end.

Kindly point out what is wrong.(or the things are going wrong in my head
?)

Kind regards,
Kazi

That got resolved.

The call to the web services required the array to be passed as:
target_ids=[]
target_ids.push(‘fr-fr’)
target_ids.push(‘de-de’)

And the Base64.encode64 worked as

file_data=Base64.encode64(File.read(file_path))

Thanks for your help :slight_smile:

On Fri, Jun 7, 2013 at 12:30 PM, Khudadad K. [email protected]
wrote:

You’ll want File.read(file_path, encoding: “BINARY”)

Cheers

robert

On Tue, May 28, 2013 at 8:30 AM, Khudadad K. [email protected]
wrote:

I am investigating why I am getting a status 200 error.

200 is not an error.

robert