Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Reading an XML Document Using XmlLite

This topic discusses how to implement an XmlLite application, and provides a simple example that reads an XML file and prints the nodes to the console. Included is an XML file that the example can read. Also included is the console output from the example.

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 content of Source: XmlLiteReader.cpp into your C++ source file.

  3. Build the example. Building the example will also create the debug directory, which is required for the next step.

  4. Copy the sample XML file to the clipboard, and paste it into your favorite editor to create an XML document. Place the document in the debug directory.

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

The following is a detailed list of what you have to do to implement an XmlLite application:

  • Instantiate an instance of a class that extends the IStream interface. This is required because the CreateXmlReader and CreateXmlWriter methods require your application to pass in an object that implements the IStream interface. The sample application that demonstrates reading using XmlLite calls the method SHCreateStreamOnFile to create the stream.

  • If you write your own class that implements the IStream interface, if possible, implement the Seek and Stat methods in your IStream class. If you implement these methods, XmlLite is able to allocate memory more efficiently, which improves performance.

  • When you want to use the XmlLite API, first declare and instantiate an instance of your class that extends IStream. Use the CComPtr class when declaring the variables that will contain the file reader and writer variables. This is better because XmlLite has the capability to use smart pointers, which handle many aspects of memory management. The following code shows how to use the CComPtr class:

    CComPtr<IStream> pFileStream;
    CComPtr<IXmlReader> pReader;
    
  • If you are using both a reader and a writer, you must declare and instantiate two instances of a class that implements IStream. The purpose of one of these will be to read the XML file and pass it to the IXmlReader. The purpose of the other is to provide the IXmlWriter with an output stream. If you are writing your own class that implements IStream, you would typically write the class in such a way that you can pass an argument when creating the instance that specifies whether the instance is for reading or for writing.

  • Declare and instantiate an instance of IXmlReader. Also use CComPtr when declaring the IXmlReader and IXmlWriter variables. The following is the declaration of IXmlReader from the example:

    CComPtr<IXmlReader> pReader;
    
  • Use the SetInput method to set the input stream of the IXmlReader to your instance of the IStream derived class. The following code shows how to use SetInput:

    if (FAILED(hr = pReader->SetInput(pFileStream)))
    {
        wprintf(L"Error setting input for reader, error is %08.8lx", hr);
        return -1;
    }
    
  • Set appropriate options on the reader and writer. For example, you might want to configure the reader to allow or prohibit the use of Document Type Definitions (DTDs) to expand entities. You might want to configure the writer so that it outputs the XML with indentation. The following is an example of setting an option on the reader to disallow processing of DTDs:

    if (FAILED(hr = pReader->SetProperty(XmlReaderProperty_DtdProcessing, DtdProcessing_Prohibit)))
    {
        wprintf(L"Error setting XmlReaderProperty_DtdProcessing, error is %08.8lx", hr);
        return -1;
    }
    
  • To use the IXmlReader, you repeatedly call the Read method until all nodes have been read. After reading each node, there are a variety of operations that you can do on the node, such as examining the qualified name of the node or retrieving its value.

Show:
© 2015 Microsoft