Processing method of binary data in XML

Source: Internet
Author: User
Tags format base64
xml| binary | Data in XML, all data is displayed as text, but binary data cannot be represented directly in text format, so how does XML handle binary data? Here is a discussion.
For simplicity and versatility, XML is designed to represent data in text format. In XML, all data is stored in text format, and binary data is no exception. In XML, binary data is encoded into a text format and sent to the destination. The destination party receives this text binary data, then with the same decoding program to solve the corresponding binary data, of course, the original format of the data, name and other ancillary information must be sent together as relevant information. General binary data encoding into BASE64 format, its advantage is easy to encode and decode, the disadvantage is more than pure binary 33% of storage space.

The following are the specific program implementations:

<%@ page contenttype= "text/html; charset=gb2312 "%>

<%@ page import= "java.io.*"%>

<%

String Ret=new string ();

try{

InputStream in=new FileInputStream ("C:\\aaa.doc");

Byte[] Bytes=new byte[in.available ()];

In.read (bytes);

Ret=new Sun.misc.BASE64Encoder (). Encode (bytes); The specific coding method

In.close ();

}

catch (FileNotFoundException e) {

E.printstacktrace ();

}

catch (Java.io.IOException ex) {

Ex.printstacktrace ();

}

%>

A RET is the final result, and the encoding can be sent in a standard XML format. Sent to the destination, but also to the corresponding decoding of the data in order to get the original binary file, decoding the code as follows:

<%@ page contenttype= "text/html; charset=gb2312 "%>

<%@ page import= "java.io.*"%>

<%

byte[] bytes = new Sun.misc.BASE64Decoder (). Decodebuffer (ret);

Java.io.ByteArrayInputStream instream=new java.io.ByteArrayInputStream (bytes);

byte[] buffer =new byte[1444];

FileOutputStream fs=new FileOutputStream ("D:\\aaa.doc");

int bytesum=0;

int byteread=0;

while ((Byteread=instream.read (buffer))!=-1) {

Bytesum+=byteread;

Fs.write (Buffer,0,byteread);

}

%>

BASE64 can handle not too large data, if you want to move a large number of data, and to consider space/time efficiency, to adopt alternative methods.





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.