Sending binary data in XML documents

Source: Internet
Author: User
Tags xml cdata


Sending binary data in XML documents
Cherami translation (Participation score: 20374, expert score: 5500) published: Version: 1.0 read:3505Times

This article is a series of zdnet translations.ArticleOne, the original article has been published on the zdnet website

XML is usually used to describe text data. For example, an element itself gives a text name, while the content of an element is usually text-based. However, sometimes you may want to put non-text data into XML documents. Let's check several methods you may use.

Problem

 
You may think that you only need to place some binary data into the start and end labels, and everything is fine. Unfortunately, this may cause several potential problems:
· New Line Characters and space characters in XML will confuse binary data.
· Binary data may contain null characters.
· Binary data may contain "</" sequences.
These problems affect both binary data and XML parsing. If the parser cannot decide how to proceed, you cannot extract any data. If the parser "formats" the data, you cannot process the binary data correctly.
Solution

 
There are at least three solutions to this problem:
· Use the CDATA label to directly embed binary data into XML documents.
· Use a URL to reference binary data.
· Encode binary data into text-based format and then set it to the content of XML elements.
Binary embedding

 
This solution allows you to directly store binary data in XML documents. Using this method, you do not need to retrieve a file from a remote system or decode the file before use. The data can be processed immediately.
To use this method, use the xml cdata tag, which is a special tag used to process data that cannot be parsed during XML parsing. Basically, you can use a start tag and an end tag to indicate the start and end positions of binary data. The element values contained in CDATA are binary data, for example:

<Product>
<SKUs> 99238 </SKU>
<Description> super gidgetidoo </description>
<Picture>
<! [CDATA [q ??????????? T ???]>
</Picture>
</Product>

As you can see, the CDATA label uses the sequence "<! [CDATA ["as the start label and"]> "as the end label. The XML Parser ignores all data between tags.
Unfortunately, this method has some problems. First, you may encounter issues with the character sets used by XML documents, parsers, and your binary data. Second, your binary data may contain the "]>" sequence, which makes the XML Parser think that it does not need to parse the data, although it is not the end of the binary data, this is a very troublesome situation.
Binary reference

 
The simplest solution is to place a binary file on a server that can be accessed over the network and reference it with a URL. Using references allows you not to worry about file encoding or sending large files in XML files over the network. It also allows you to dynamically update files without sending a new XML document. The following is an example:

<Product>
<SKUs> 99238 </SKU>
<Description> super gidgetidoo </description>
<Picture> HTTP:// Www.mysupergidgets.com/pictures/99238.jpg </picture>
</Product>

Binary encoding

 
There are a few ways to encode binary data into text data. Basically, this process uses a relatively simpleAlgorithmModify binary data to ASCII bytes. The two most popular binary encoding algorithms are uuencode and base64 encoding.
An extended version of binary encoding called mime is used to add the encoding information of a file (such as a file name ). EncodingProgramIt is easy to get. It is generally used as a shared software and programming tool. The following is an example of embedding a binary-encoded file into an XML document:

<Product>
<SKUs> 99238 </SKU>
<Description> super gidgetidoo </description>
<Picture>
Content-Description: file encoded with encode64.exe.
Content-Disposition: attachment; filename = "foo.zip"
Content-transfer-encoding: base64
Content-Type: Application/octet-stream

uesdbbqaaaaiagtams2/release
zjaaiwca8re + CIAB/release + 7
release
eaaqa4aaaasaaaaaaa

BR>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.