Click here to Skip to main content
Click here to Skip to main content

A simple STL based XML parser

By , 16 May 2000
 
  • 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

    About the Author

    David Hubbard
    United States United States
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    QuestionHow to parse XML file PinmemberRajiv_cn14 Jul '08 - 23:05 
    GeneralHi. I want your help, David. PinmemberChoi so-dam23 Apr '08 - 14:38 
    NewsHere with Standard C++ only... Pinmemberpietrom14 Dec '06 - 5:28 
    GeneralCompiling using MinGW PinmemberEmili___3 Mar '06 - 2:50 
    Questionvc2005 compatibility? PinmemberBryanWilkins19 Dec '05 - 10:42 
    AnswerRe: vc2005 compatibility? PinmemberJeff_rowa22 Nov '06 - 1:24 
    Hi Bryan,
    I have the same problem, whenever I want to compile the project under VS2005 I get lots can't convert error, could you say what did u do or post your code here please.
     
    Regards,
    GeneralW3C compliance PinmemberUmut Alev25 Jul '05 - 21:04 
    QuestionHow do you I get attribute value ? Pinmemberkhmerkidnow29 Jul '04 - 14:07 
    AnswerRe: How do you I get attribute value ? Pinmemberdbpradeep19 Mar '09 - 0:29 
    Generalmultiple entries of the same name Pinmemberburnettb31714 Jul '03 - 11:58 
    GeneralRe: multiple entries of the same name Pinmembermikeguz14 Oct '05 - 17:47 
    GeneralNodes have the same name BUG PinmemberKen Lee9 Apr '03 - 20:43 
    GeneralRe: Nodes have the same name BUG PinmemberKen Lee9 Apr '03 - 20:52 
    Generalpretty print of stream Pinmemberfazer777 Feb '03 - 18:00 
    Generalattributes Pinmemberfazer777 Feb '03 - 17:56 
    GeneralBug fix for null nodes PinmemberBenJeremy2 Jan '03 - 10:06 
    GeneralRe: Bug fix for null nodes PinmemberBenJeremy6 Jan '03 - 5:04 
    GeneralAnoying bug for an xml parser Pinmemberyarp21 Nov '02 - 20:42 
    GeneralRe: Anoying bug for an xml parser Pinmemberyarp21 Nov '02 - 21:17 
    GeneralSearch PinmemberDavid Frame7 Feb '02 - 5:18 
    GeneralRe: Search Pinmemberifgarcia20 Apr '05 - 21:55 
    Generalanother bug in XmlParser.h PinmemberPika27 Jul '01 - 23:47 
    Questionnotify on end-of-node? PinmemberChris Losinger28 Mar '01 - 17:44 
    AnswerRe: notify on end-of-node? Pinmembergok17 Feb '03 - 17:11 
    GeneralRe: notify on end-of-node? Pinmemberkhmerkidnow29 Jul '04 - 13:50 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.6.130523.1 | Last Updated 17 May 2000
    Article Copyright 2000 by David Hubbard
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid