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:

  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


Comments

  • you need a fix for complex structures

    Posted by Legacy on 05/25/2003 12:00am

    Originally posted by: Frans Nagel

    The code as you write it doesn't work for complex xml structures. I had to rewrite part of it and use recursion to create nodes. I suggest that instead of raising events as you do instead you build up a tree consisting of child and parentnodes.
    class XmlNode;

    typedef std::vector<XmlNode*> XmlNodeVector;
    class XmlNode
    {
    ...

    string m_strNodeName;
    string m_strNodeValue;
    XmlNode * m_pParent;
    XmlNodeVector m_vecChildren
    };
    That's what I did based on your code and now it works perfectly so thank you for your work.

    Reply
  • Parser code have error

    Posted by Legacy on 07/10/2001 12:00am

    Originally posted by: Andrey

    <c><c>some text</c></c> will be parsed incorrectly.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds