Send binary data in XML

Source: Internet
Author: User
Tags xml cdata
Author: Builder. com

XML is generally considered a way to describe data using text. For example, an element provides a text name. The content of an element is usually text-based. But sometimes you need to input data instead of text to your XML document. Let's consider your possible options.

Problem:

You may think that you can just put some binary data into the start and end labels, and you have done the same. However, unfortunately, this may cause some potential problems:

    • In XML, a new line and a space character will disrupt binary data.
    • Binary data may contain null characters.
    • Binary data may contain </sequence.

These problems not only affect binary data, but also affect XML analysis. If the analyzer cannot determine what is being calculated, you cannot obtain any data. If the data is formatted by the analyzer, 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 view binary data.
    • Encodes binary data into a text-based format so that it can be used as the content of an XML element.
Binary embedding:

If you choose to embed binary data into an XML file, you do not have to drag a file from a remote resource or decode it before use. The data can be used immediately.

Xml cdata labels are used to use this method. xml CDATA labels are special labels for processing data not analyzed in XML. Essentially, you use a set of start and end tags to indicate the start and end positions of binary data. The value of the element that contains CDATA is the binary data.

As you can see, the CDATA tag places the string<! [CDATA [As the start tag, and the string]>Used as the end label. The XML analyzer ignores all data between the two tags.

Unfortunately, this method has some problems. First, you may find that there is a problem with XML documents, analyzer, and the set of symbols used in your binary data. Second, your binary data may contain]>String. This sequence will tell the XML analyzer where data does not need to be analyzed, even if it is not really the end of binary data-this is messy.

Binary query:

The simplest solution is probably to put the binary file on an Internet-connected server, and then you only need to check it with a URL. Using this check method eliminates the need to worry about encoding files or transmitting large files and XML files over the network. This method also allows you to dynamically upgrade files without sending a new XML document.

Binary encoding:

You can also select one of the few ways to encode binary data into text data. In essence, such a processing process transfers binary bytes through a relatively simpleAlgorithmConverted to ASCII bytes. The two most common binary encoding algorithms are UU encoding and base64 encoding.

An extended version called mime binary encoding is added to the information of the encoded file, such as the file name. EncodingProgramIt is easy to find as a shared software and programming tool.

In addition to text data, inputting other types of data into an XML file is also useful in many cases. However, this is not as simple as directly placing data into a group of start and end tags, because putting data into the start and end tags will cause problems in both data and XML analysis. Fortunately, you can use one of the three solutions-binary embedding, binary checking, and binary encoding-to allow your XML document to successfully include data, not just text.

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.