Click here to Skip to main content
6,633,937 members and growing! (21,500 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Parsers     Intermediate

A simple STL based XML parser

By David Hubbard

This is a small non-validating XML parser based purely on STL
VC6, STL, Dev
Posted:16 May 2000
Views:172,423
Bookmarked:62 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
48 votes for this article.
Popularity: 6.41 Rating: 3.81 out of 5
3 votes, 13.6%
1
2 votes, 9.1%
2
1 vote, 4.5%
3
7 votes, 31.8%
4
9 votes, 40.9%
5
  • 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


    Member

    Location: United States United States

    Other popular Algorithms & Recipes articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
    GeneralHow to parse XML file PinmemberRajiv_cn0:05 15 Jul '08  
    GeneralHi. I want your help, David. PinmemberChoi so-dam15:38 23 Apr '08  
    NewsHere with Standard C++ only... Pinmemberpietrom6:28 14 Dec '06  
    GeneralCompiling using MinGW PinmemberEmili___3:50 3 Mar '06  
    Generalvc2005 compatibility? PinsupporterBryanWilkins11:42 19 Dec '05  
    GeneralRe: vc2005 compatibility? PinmemberJeff_rowa2:24 22 Nov '06  
    GeneralW3C compliance PinmemberUmut Alev22:04 25 Jul '05  
    GeneralHow do you I get attribute value ? Pinmemberkhmerkidnow15:07 29 Jul '04  
    GeneralRe: How do you I get attribute value ? Pinmemberdbpradeep1:29 19 Mar '09  
    Generalmultiple entries of the same name Pinmemberburnettb31712:58 14 Jul '03  
    GeneralRe: multiple entries of the same name Pinmembermikeguz18:47 14 Oct '05  
    GeneralNodes have the same name BUG PinmemberKen Lee21:43 9 Apr '03  
    GeneralRe: Nodes have the same name BUG PinmemberKen Lee21:52 9 Apr '03  
    Generalpretty print of stream Pinmemberfazer7719:00 7 Feb '03  
    Generalattributes Pinmemberfazer7718:56 7 Feb '03  
    GeneralBug fix for null nodes PinmemberBenJeremy11:06 2 Jan '03  
    GeneralRe: Bug fix for null nodes PinmemberBenJeremy6:04 6 Jan '03  
    GeneralAnoying bug for an xml parser Pinsupporteryarp21:42 21 Nov '02  
    GeneralRe: Anoying bug for an xml parser Pinsupporteryarp22:17 21 Nov '02  
    GeneralSearch PinmemberDavid Frame6:18 7 Feb '02  
    GeneralRe: Search Pinmemberifgarcia22:55 20 Apr '05  
    Generalanother bug in XmlParser.h PinmemberPika0:47 28 Jul '01  
    Generalnotify on end-of-node? PinmemberChris Losinger18:44 28 Mar '01  
    GeneralRe: notify on end-of-node? Pinmembergok18:11 17 Feb '03  
    GeneralRe: notify on end-of-node? Pinmemberkhmerkidnow14:50 29 Jul '04  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 16 May 2000
    Editor: Chris Maunder
    Copyright 2000 by David Hubbard
    Everything else Copyright © CodeProject, 1999-2009
    Web22 | Advertise on the Code Project