How to Embed Binary Data in Xml

How do you embed binary data in XML?

You could encode the binary data using base64 and put it into a Base64 element; the below article is a pretty good one on the subject.

Handling Binary Data in XML Documents

How to embed binary data in XML using XML Builder?

I think you need to do something with the binary stream, because it has some invalid characters for XML.

Here is a document about handle binary data in xml, it's language independent, just xml.

In this case, I think the easiest way to handle your attachment is using base64.
If @invoice.attachment is the binary string, use the code below, if not, get it:

xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.invoice(:invoice_no => @invoice.number) do
xml.pdf Base64.encode64(@invoice.attachment)
end

When you read the xml, decode the string with base64 format and get the binary string.

Wish it helps.

Encoding binary data within XML: Are there better alternatives than base64?

I have developed the concept in a C code.

The project is on GitHub and is finally called BaseXML: https://github.com/kriswebdev/BaseXML

It has a 20% overhead, which is good for a binary safe version.

I had a hard time making it work with Expat, which is the behind the scene XML parser of Python (THAT DOESN'T SUPPORT XML1.1!). So you'll find the BaseXML1.0 Binary safe version for XML1.0.

I will maybe release the "for XML1.1" version later if requested (it is also binary safe and have a 14.7% overhead), it's ready and working indeed but useless with Python built-in XML parsers so I don't want to confuse people with too many versions (yet).

Include binary data in XML file

Have a look at this article:
http://www.codeproject.com/KB/XML/xml_serializationasp.aspx
it describes saving bmp images into and out of xml by converting them to byte arrays. I suspect a similar method would work for other files.

Convert a file to binary for insertion in an xml file

Here's the solution I found. I'm using this to convert files to what's needed in this case which is Base64:

Function EncodeBase64ForString(strString As String) As String
Dim arrData() As Byte

arrData = StrConv(strString, vbFromUnicode)

Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement

Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")

objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64ForString = objNode.text

Set objNode = Nothing
Set objXML = Nothing
End Function

Public Function GetFileBytes(ByVal strPath As String) As Byte()
Dim lngFileNum As Long
Dim bytRtnVal() As Byte

lngFileNum = FreeFile

If LenB(Dir(strPath)) Then ''// Does file exist?
Open strPath For Binary Access Read As lngFileNum
ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
Get lngFileNum, , bytRtnVal
Close lngFileNum
Else
Err.Raise vbObjectError + 1, , "Could not find " & strPath
End If

GetFileBytes = bytRtnVal

Erase bytRtnVal
End Function

Use CDATA to store raw binary streams?

You can store it as CDATA, but there's the risk that some byte sequences will evaluate to valid XML that closes the CDATA section. After a quick look at http://www.w3.org/TR/2006/REC-xml-20060816/#sec-cdata-sect, it seems you can have any sequence of chars except "]]>". Have a look at what is a valid XML char too.

How to decode binary data embedded in XML using XSLT?

Recent releases of Saxon (PE and EE) include an implementation of the EXPath binary module (http://expath.org/spec/binary) which contains everything you need to manipulate binary data - except of course a specification of the binary data that you want to manipulate. If you know what the input structure is, and if you know what the output you want to produce should look like, then the binary functions should help you, but I fear from your question that you don't really know either.

If you believe that the binary data is, for example, a base64-encoded JPEG file, then you don't actually need the EXPath binary module - the EXPath file module (also implemented in Saxon PE and EE) should be enough. See http://expath.org/spec/file#fn.write-binary

You can do:

file:write-binary("output.jpeg", xs:base64Binary(jpegBitMap))

to write the contents of a binary element as an external file, and then you can try to open the file using an application that understands the relevant format.

(Be careful with these methods because they have side-effects, which doesn't fit very well into XQuery or XSLT. For example, don't try calling them within a variable initializer which won't get called if the variable is never used.)

how to send binary data within an xml string

Base64 representation is universaly used to represent binary data.

public void EncodeWithString() {
System.IO.FileStream inFile;
byte[] binaryData;

try {
inFile = new System.IO.FileStream(inputFileName,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0,
(int)inFile.Length);
inFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message);
return;
}

// Convert the binary input into Base64 UUEncoded output.
string base64String;
try {
base64String =
System.Convert.ToBase64String(binaryData,
0,
binaryData.Length);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Binary data array is null.");
return;
}

// Write the UUEncoded version to the XML file.
System.IO.StreamWriter outFile;
try {
outFile = new System.IO.StreamWriter(outputFileName,
false,
System.Text.Encoding.ASCII);
outFile.Write("<BinaryFileString fileType='pdf'>");
outFile.Write(base64String);
outFile.Write("</BinaryFileString>");
outFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message);
}
}

At the receiving end you can reverse this and get back original file content as mentioned below.

        // Convert the Base64 UUEncoded input into binary output.
byte[] binaryData;
try {
binaryData =
System.Convert.FromBase64String(base64String);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Base 64 string is null.");
return;
}
catch (System.FormatException) {
System.Console.WriteLine("Base 64 string length is not " +
"4 or is not an even multiple of 4." );
return;
}

Storing C++ binary output in xml

You need to encode the raw data to one of the two data types. This is to keep some random data from messing up the XML format, for example if you had a < embedded in the data somewhere.

You can choose whichever of the two is most convenient for you. The hexadecimal type is easier to write code for but produces a larger file - the ratio of bytes out to bytes in is 2:1, where it is 4:3 for the Base64 encoding. You shouldn't need to write your own code though, Base64 conversion functions are readily available. Here's a question that has some code in the answers: How do I base64 encode (decode) in C?

As an example of how the codings differ, here's the phrase "The quick brown fox jumps over the lazy dog." encoded both ways.

Hex:

54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e

Base64:

VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=

Write to, and read binary data from XML file in .Net 2.0

You seem to write some bad XML - use the following to write the data:

        w.WriteStartDocument();
w.WriteStartElement("pstartdata");
#region Main Data
w.WriteElementString("pdata", "some data here");

// Write the binary data
w.WriteStartElement("bindata");
w.WriteBase64(fileData[1], 0, fileData[1].Length);
w.WriteEndElement();
#endregion (End) Main Data (End)

w.WriteEndDocument();
w.Flush();
fs.Close();

For reading you will have to use XmlReader.ReadContentAsBase64.

As you have asked for other methods to write and read binary data - there are XmlTextWriter.WriteBinHex and XmlReader.ReadContentAsBinHex. BEWARE that these produce longer data than their Base64 pendants...



Related Topics



Leave a reply



Submit