Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A simple STL based XML parser

0.00/5 (No votes)
16 May 2000 1  
This is a small non-validating XML parser based purely on STL
  • Download source files - 10 Kb
  • This is a small XML parser, based purely on STL. There are two main classes XmlStream and XmlParser. XmlParser.h contains most of the parsing code. It has several state variable, which can be split up into two categories:

    1. Buffer state - Shows where we are parsing from
    2. Parsing state - Shows what we have found
    XmlParser makes extensive use of offsets to keep track of its state. This is done by design. In order to maximize speed it does not do any string copies.

    To start parsing declare an instance of the class XmlStream and setup the buffer that you want to parse. An example is included in Parser.cpp. Call parse in XmlStream, passing in a pointer to the buffer and the buffers length. You will see screen output showing what has been found. This is simple debug output and can be turned off.

    XmlNotify is used as an interface class to notify a subscriber of nodes and elements being found. There is a pointer to a subscriber in the XmlStream class. The subscriber can be set using setSubscriber.

    Notice that no XML document declaration is included nor is a schema included. If those exist in your buffer don't send them to the parser. Later, the ability to remove these, will added in the code to step through these. so this is a non-validating parser. There is one bug in the parser. When an empty node is encountered it will be reported as an element this will be fixed later. An example of this is included in the sample code.

    If you have any suggestions or improvements let me know.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here