STL-based (non-validating) XML Parser | CodeGuru

STL-based (non-validating) XML Parser

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: buffer state – shows where we are parsing from parsing state – shows what we have found […]

Written By
CodeGuru Staff
CodeGuru Staff
May 13, 2000
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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, setup the buffer
that you want to parse. An example is included in Parser.cpp. Then 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 nonvalidating 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.

Downloads

Download source – 11 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.