Efficient XML document exchange

Source: Internet
Author: User
XML documents tend to become wordy due to their inherent descriptive features. The result is that the document will become longer due to the increase in the data to be described, and such a large document will present the title when it needs to be exchanged with other entities. And other documents

XML documents tend to become wordy due to their inherent descriptive features. The result is that the document will become longer due to the increase in the data to be described, and such a large document will present the title when it needs to be exchanged with other entities. Compared with other documents (such as flat files or Electronic Data Interchange (EDI), XML documents are particularly lengthy. To illustrate this concept, let's take a look at the following common text files: John, Doe, 1587,4/18/2000, 1234

Anywhere St., SomeCity, AZ, 85222

Let's look at this XML document:

Reference content is as follows:

John
Doe
4/18/2000

1234 Anywhere St.

SomeCity
AZ
85222

If you have processed many XML documents, so you won't be surprised that even if this XML document and this normal text file separated by commas contain the same raw data ), XML documents are much larger than common text files. After all, XML is a metadata language (it contains many advantages, such as support for parsing, verification, conversion, and so on ), therefore, the size is much larger than that of other similar documents. Because XML is more widely used as a data exchange method, there is no doubt that the size of the exchanged documents will decrease the performance and scalability of the application.

There are many ways to minimize the size of an XML document, such as converting an element to an attribute (abbreviated element and attribute name) (where appropriate), to remove unimportant vacancies, only some content is defined. However, no matter what changes you make, the ultimate large volume of raw data will still form a large XML document. If your XML document contains many megabytes, how can you effectively transmit them in your enterprise or transfer them to other enterprises?

One way is to divide a large XML document into multiple documents, which runs well (if Sharding is possible, however, this will produce additional complexity and ensure that all documents can be accurately sent and received. Even small documents that have been removed may form documents of several megabytes due to a large amount of transferred data. Since these ambush headers exist, how can we XML developers exchange XML data more effectively? (I agree to play golf .)

You can use the compression technique to accelerate document exchange between points. Because XML is a simple text form, large documents can be compressed into a small form. The sample program shown here demonstrates how to compress XML documents by adding a. Net component of the development code to a ZIP archive file. This can minimize the file size and improve the efficiency of data exchange.

Although the. Net J # language supports compression, the base class libraries built into the. Net Framework do not support compression. However, a component named SharpZipLib that is fully written by the control code can be used to compress various types of documents (download this component in www.icsharpcode.net/opensource/sharpziplib/default.asp ). SharpZipLib is a class library written in C # and used in. Net to support Zip, GZip, Tar, and bzip2. It is implemented as an assembly, and it can also be used together with any project that uses the. Net language.

I have applied the early beta release of SharpLibZip in several exploitation programs, and I think it is very effective in compressing and extracting documents. Let's take a look at how to apply the SharpZipLib component to compress XML documents with programs.

Compressing XML documents

Although SharpZipLib can perform several types of compression, I decided to use the most extensive ZIP compression pattern in the typical program, because it is famous and useful. To make the code reusable, I wrote a custom class named Zipper. Zipper has a static method named GenerateZipFile () (which can receive the path of the specified ZIP file to be retained) and An ArrayList containing all file paths to be compressed (see list 1 ).

The Zipper class is an encapsulation class named ZipOutputStream in SharpZipLib. You can use Zipper to compress multiple files into a simple ZIP file (a file with a ZIP extension name) without writing any code or spending any effort ). The GenerateZipFile () method is to compress by creating an instance of the ZipOutputStream class and setting the compression level through its SetLevel attribute. The maximum compression level can be set to 9, while the minimum is 0.

After the compression level is set, the content of the file specified by the ArrayList (uploaded to GenerateZipFile () method will be processed. An inherent counter (enumerator) lists objects in the list one by one. Each file is loaded into a ZipEntry object that receives the file name and logon time. Then, the ZipEntry object is added to the ZipOutputStream object through the PutNextEntry () method.

After the file name is added to this ZIP archive file, it will be read through a FileStream object. FileStream (located in the namespace of System. IO) is used to read files into the buffer zone in bytes. You can call the Read () method in the FileStream object to complete Read control. Bytes in the buffer are written into the ZipOutputStream object through the Write () method. Note that the Write () method receives the length of the byte to be written into the data stream and the starting position in the buffer zone. This process is applicable to all files contained in the ArrayList parameter passed to the GenerateZipFile () method. After all entries are added to the ZIP file, it will be retained to the hard disk as a ZIP file extension name.

List 2 shows a simple ASP. Net exploitation code used to test the Zipper class (see ). It starts from defining the path of the XML document to be compressed and the path for storing the ZIP file. Although there is only one compressed XML document in this example, the paths of other documents can be added to the ArrayList object for compression. After all file paths are defined, the static method GenerateZipFile () will be called (). Once the ZIP file is created, an e-mail will be sent to the end user through the class in the System. Web. Mail namespace.

Decompress the XML document

The ability to compress XML documents can be very useful in different situations, but this situation is inevitable: someone sent you a compressed file that needs to be expanded (extracted) before parsing. This title can be solved directly by applying a class named ZipFile in SharpZipLib. In list 3, you can see that there is a static method named ExtractZipFile () in the Zipper class that expands the compressed file to a specified directory. The code first uploads a FileStream object (obtained by calling the File. Open () method) to the ZipFile class constructor to create a ZipFile instance. After the object is created, each ZipEntry in the ZIP file will be listed (enumerate. Call the GetInputStream () method of the ZipFile object. this method receives a ZipEntry to be expanded as a parameter. The data stream returned from GetInputStream () is read into a buffer zone, which is written into a file through a FileStream. When GetInputStream () is called, the ZipFile class automatically decompress the ZipEntry.

After the ExtractZipFile () method is called, all compressed files in the ZIP file will be expanded and stored on the hard disk. In addition, the extracted byte stream is written into a MemoryStream object, which is useful when the file does not need to be retained to the hard disk before being parsed.

Although XML is a lengthy Metadata Language, large documents can be compressed into a small document after applying. Net components (such as SharpLibZip. By compressing these documents, you can shorten the time for document exchange between different entities, and the result is faster data processing. If you want to try this compress/decompress code example, you can visit www.xmlforasp.net/codeSection.aspx? CsID = 95.

About Author:
Dan Wahlin (ASP. NET (Microsoft MVP) is the president of Wahlin Consulting LLC and founded XML for ASP. NET Developers website (www. XMLforASP. NET). It is important to study how.. Net platform. He is also a co-trainer and speaker and teaches public and on-site XML and. Net training courses throughout the United States. Dan is the co-author of Professional Windows DNA (Wrox), ASP. NET Tips, Tutorials, and Code (Sams), and is the author of XML for ASP. NET Developers (Sams. His approach is dwahlin@xmlforasp.net.

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.