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 filememberRajiv_cn14 Jul '08 - 23:05 
    Hi to Everyone,
     
    First Thank a lot for this code. Now I am a beginner in this XML parsing area. I want to parse an XML file so what i need to do ?
     
    I am looking forward from you.
     
    Thanks & Regards,
     
    Rajiv
    GeneralHi. I want your help, David.memberChoi so-dam23 Apr '08 - 14:38 
    Hi,
    First of all, Thanks for your posting.
    I hope to use your code for my own code.
     
    Now, I wonder about your code's license, whether it takes "CPOL" or not.
     
    Waiting for your reply.
    Thank you.
    NewsHere with Standard C++ only...memberpietrom14 Dec '06 - 5:28 
    This program is not a Standard C++ program because of the non standard libraries it uses. OMG | :OMG:
     
    Anyway it is really easy to make it use the STL only.
    I did it and it seems to work (using gcc). Poke tongue | ;-P
     
    I cannot find a way to attach a file from here, so if someone should be interested, just let me know and I will send you the code directly.
     
    --> pietromele@yahoo.com
     

    GeneralCompiling using MinGWmemberEmili___3 Mar '06 - 2:50 
    I needed to modify something to compile (using Code::Blocks) with mingw. First of all, I couldn't use atlbase.h I defined only windows.h. Second, I had to change this kind of lines in XMLParser.h:
    string::iterator start = _buffer + offset;
     
    for this:
    string::iterator start = ((string)_buffer).begin() + offset;
     
    That's all. Thanks for your code Smile | :)
     


    Questionvc2005 compatibility?memberBryanWilkins19 Dec '05 - 10:42 
    has anyone tried to make this code run on vc2005?
    I get alot of cannot convert type string to iterator etc etc... I finally got around the problems
    by not using this code, but I find myself wondering why the new compiler is so string on converting pointers to iterators.
     
    Bryan
    AnswerRe: vc2005 compatibility?memberJeff_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 compliancememberUmut Alev25 Jul '05 - 21:04 
    Perhaps this has been talked a lot but I can not stop my self from writing this.
    One of the reasons of XML is connecting platforms, apps through text encoded messages, to achieve this you need compliance. Implementing a fully (95%-98%) compliant XML parser is really hard. Usually an average Dev can get to 80% pretty quickly and the experienced can get to %90. The rest is all egde cases and getting them right requires extensive testing suites to ensure compat between different platforms. I know this by experience. There are quite a few xml parser implementations out there. Users are usually best served when the xml parser implementations are recognized by the W3C Recommendations. Other implementations can not go any further than academic studies as the cost of them making interop with the rest of the world sky rockets as dev's are trying to achieve %95 compat.
    QuestionHow do you I get attribute value ?memberkhmerkidnow29 Jul '04 - 14:07 
    If I have xml scheme like below and I like to get the attribute value such as 22805, SA287430035 or ENV_LAN_1. How would I do it? thankSmile | :) in advance.
    --------------------------------------------------------------------------------
    <?xml version="1.0" encoding="utf-8"?>
    <LOADBOX>
    <row BoxID="22805" BoxSerialNumber="SA287430035" BoxTest="ENV_LAN_1">
    <row BoxID="77688" BoxSerialNumber="SA287460015" BoxTest="ENV_LAN_5">
    <row BoxID="15141" BoxSerialNumber="HK184700609" BoxTest="ENV1">
    </LOADBOX>
    AnswerRe: How do you I get attribute value ?memberdbpradeep19 Mar '09 - 0:29 
    Me too require solution for the same problem. Can anyone help!!
    I use java.
    Generalmultiple entries of the same namememberburnettb31714 Jul '03 - 11:58 
    thanks for the great XML tools, they are very easy to use.
     
    I would like to have a table represented in XML but this implementation of the parser doesn't handle multiple entries of the same name, for example:
     
    <HOST>
    <NAME>cod</NAME>
    <DNSNAME>cod.somecorp.com</DNSNAME>
    <NIC>
       <MAC>0040f4612820</MAC>
       <IP>192.168.172.54</IP>
       <IP>192.168.173.54</IP>
    </NIC>
    </HOST>
    <HOST>
    <NAME>drum</NAME>
    <DNSNAME>drum.somecorp.com</DNSNAME>
    <NIC>
       <IP>192.168.172.47</IP>
    </NIC>
    </HOST>
     
    in this example the <HOST> and <IP> tags both have multiple entries.   the XMLDialog displays the host cod for both entries, and only 1 IP address, it would be nice to be able to address these in the read/write methods like:
     
    "HOST[0]:NIC:IP[1]","192.168.173.54"
     

     
    --Ben Burnett

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

    Permalink | Advertise | Privacy | Mobile
    Web04 | 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