Reading an XML Document Using Chunking

Chunking is a technique where instead of getting the value for an element or an attribute all at once, the module using XmlLite has the option to retrieve a maximum amount of data in a single method call. If the element or attribute has more data than the specified maximum, chunking allows the data to be retrieved in multiple sequential calls to a method.

This topic illustrates how to implement an XmlLite application that reads an XML document using the chunking functionality.

When XML documents can be very large, reading with chunking allows you to write an application that can read the large document while maintaining a predictable memory usage profile.

This example includes the following files:

To create the example

  1. Create a Visual Studio 2005 project. For details of how to do this, see Building XmlLite Applications

  2. Copy the C++ source code for this example and paste it into your C++ source file.

  3. Build the example.

  4. Copy the sample XML file and create an XML document. Place the document in the debug directory.

  5. Run the sample and verify that the output matches the output provided.

Programming Details

The key difference in implementing an application that uses chunking is that when your application reads the value of an attribute or an element, you use the ReadValueChunk method instead of the GetValue method. Typically, your application would call ReadValueChunk in a loop, iterating until the method indicates that no more data is available by returning S_FALSE.

This example uses an artificially small chunk size for demonstration purposes. When reading very large documents, you will want to use a chunk size large enough to minimize the number of calls to ReadValueChunk.

Show: