Click here to Skip to main content
13,831,402 members
Click here to Skip to main content
Add your own
alternative version


58 bookmarked
Posted 24 Aug 2001

Object-oriented XML Parser

, 28 Aug 2001
Rate this:
Please Sign up or sign in to vote.
Object-oriented parser to read/write XML files using MSXML parser


I had a task to write an XML parser to read/write XML files with list of possibilities (requirements).


  • Comfort for developer (user of parser)
  • Possibility of recording and reading using the same instance of parser
  • No use of MFC
  • User object writes and reads itself
  • Possibility to write in binary stream, without changing source of user objects
  • Possibility of creation of proxy classes for reading/writing of user classes, modification of which is forbidden
  • Same code in user object for reading and writing
  • Possibility of reading/writing simple types
  • Possibility of reading/writing objects (with possibility of determination of type during loading)
  • Possibility of reading/writing vector, map of simple and complex objects
  • Possibility of reading/writing the attributes
  • Absences of restrictions on nesting of one type in others
  • Minimization of operations "if"
  • Unimportance of order of tags in parent tag
  • Absence of support namespace and schema


Based on several ideas:

  1. Using of parser-a based on sample from Windows Platform SDK
  2. Each user object that understand that it is his time to be read, throws itself just like "throw this"
  3. Than parser catch him and put into stack and all calls will be send to this object, until anyone else throw itself or end of object will be found
  4. Each user object have only one function "determine(ProxyObject& proxy)" it should ovewrite
  5. Proxy - it is an abstract object that have some virtual functions to read/write some types of data and proxy implementations which are made automaticaly by parser to exactly "know" the type of stream and current operation
  6. There is a template class with template constructor, that gets members of user objects, proxy object, "name" of the object to read
  7. This template class understands itself what to do with that members and call corresponding proxy's functions.
  8. There are some proxy classes that can read/write string, int and float.

Here is that wonderful template:

template <class T = void*>
class determineMember
    template <class OT>
    determineMember(OT& t, 
        const char* str,XmlParserProxy& p,T* _t=0) throw(XmlObject*)

Currently my parser satisfies all requirements above. And this is example of user objects:

struct Nodes : XmlObject
    vectorDel<Node*>  m_nodes;
    virtual void determine(XmlParserProxy& p) throw(XmlObject*)
struct Pipeline : XmlObject
  string m_name;
  int    m_count;
  float  m_sum;
  SomeObject* m_someObject;
  vector<string> m_transitions;
  vector<int> m_point;
  Nodes m_nodes;
  map<int,NodeDisplay> m_map;
  virtual void determine(XmlParserProxy& p) throw(XmlObject*)

Note that you can have pointers and vectors of objects and have different classes there.

This is an example of using the parser:


Document doc;
XML::ZXmlParserImpl parser(&doc);



Look at this sample XML file:

   <!-- string -->
        <!-- int -->
        <!-- float -->
        <!-- object having type from set of types-->
         <!-- uncomment FirstObject and comment SecondObject 
                     to make FirstObject instead of SecondObject
        <!-- new XMLObject of type Nodes-->
                <!-- insert in vector<Node> new StartNode, 
                                               string attribute-->
                <start-node id="Start">
                        <!-- insert vector<NodeDisplay> new NodeDisplay-->
                <!-- insert in vector<Node> 
                        new InteractionNode, string attribute-->
                <interaction-node id="It">

                    <!-- two string attributes-->
                   <template dynamic="false" index="2">

<!-- vector of strings -->

<!-- vector of ints -->

<!-- and now - std::map<int,NodeDisplay> -->


This table shows how to implement determine function with different members

Member variableSource in determineComments
string m_name;determineMember<>(m_name,"name",p); 
string m_name;determineMember<AtribValue>(m_name,"name",p);Use AtribValue to indicate that name is attribute but not a node
int m_count;determineMember<>(m_name,"count",p); 
float m_sum;determineMember<>(m_name,"sum",p); 
SomeObject* m_someObject;determineMember <FirstObject>(m_someObject, "FirstObject",p);If tag FirstObject found, then new FirstObject will be assigned to m_someObject
SomeObject* m_someObject;determineMember <SecondObject>(m_someObject, "SecondObject",p);If tag SecondObject found, then new SecondObjectwill be assigned to m_someObject
SomeObject m_nodes;determineMember<>(m_nodes,"nodes",p); 
std::vector<int> m_point;determineMember<int>(m_point,"point",p); 
std::vector<string> m_transitions;determineMember <string>(m_transitions, "transition",p); 
std::map<int,SomeObject> m_map;determineMember<>(m_map,"Map",p); 
std::vector<Node*> m_nodesdetermineMember<StartNode>(m_nodes,"start-node",p);If tag start-node found, then new StartNode will be made and inserted in m_nodes
std::vector<Node*> m_nodes;determineMember <InteractionNode>(m_nodes, "interaction-node",p);If tag interaction-node found, then new InteractionNode will be made and inserted in m_nodes

In demo project I have shown how to implement classes for reading/writing std::map not in parser's source files.


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

Mike Melnikov
Web Developer
Russian Federation Russian Federation
Mike has been programming in C/C++ for 11 years and Visual C++/MFC for 4 years. His background includes pure and applied mathematics, engineering and physics, and he is currently based in Moscow, Russia.

You may also be interested in...

Comments and Discussions

GeneralCompile errors Pin
Member 6681898-May-08 14:14
memberMember 6681898-May-08 14:14 
QuestionHo do i add MSXMl6.dll to my installer ? Pin
code4jigar5-Sep-06 3:18
membercode4jigar5-Sep-06 3:18 
AnswerRe: Ho do i add MSXMl6.dll to my installer ? Pin
Mike Melnikov29-Sep-06 15:01
memberMike Melnikov29-Sep-06 15:01 
GeneralWriting to an XML file Pin
wcovuaku28-Oct-04 6:56
memberwcovuaku28-Oct-04 6:56 
GeneralRe: Writing to an XML file Pin
Mike Melnikov28-Oct-04 20:07
memberMike Melnikov28-Oct-04 20:07 
QuestionHow to serialize custom map&lt;CString,Object** &gt;? Pin
vgrigor13-Oct-04 23:23
membervgrigor13-Oct-04 23:23 
Generalsome problems: map - error Pin
vgrigor13-Oct-04 21:41
membervgrigor13-Oct-04 21:41 
GeneralHelp please Pin
Anonymous25-Aug-04 23:38
memberAnonymous25-Aug-04 23:38 
GeneralRe: Help please Pin
Colin Angus Mackay26-Aug-04 0:43
memberColin Angus Mackay26-Aug-04 0:43 
GeneralVisual Studio .NET 2003 Pin
Sevig11-Jul-04 21:22
memberSevig11-Jul-04 21:22 
GeneralRe: Visual Studio .NET 2003 Pin
Mike Melnikov25-Aug-04 23:55
memberMike Melnikov25-Aug-04 23:55 
Generallitle trouble with charsets Pin
Member 39149015-Jan-04 22:47
memberMember 39149015-Jan-04 22:47 
GeneralRe: litle trouble with charsets Pin
Mike Melnikov15-Jan-04 23:56
memberMike Melnikov15-Jan-04 23:56 
GeneralRe: litle trouble with charsets Pin
aGodInHost1-May-04 6:30
memberaGodInHost1-May-04 6:30 
GeneralProblem with MFC release.. Pin
EPulse21-Dec-03 18:45
memberEPulse21-Dec-03 18:45 
GeneralRe: Problem with MFC release.. Pin
EPulse21-Dec-03 21:52
memberEPulse21-Dec-03 21:52 
GeneralLimitation Pin
Sahbi15-Jun-03 1:45
memberSahbi15-Jun-03 1:45 
GeneralRe: Limitation Pin
Mike Melnikov25-Jun-03 1:49
memberMike Melnikov25-Jun-03 1:49 
Generalabout attribute and one advise Pin
marco.song13-May-03 17:04
membermarco.song13-May-03 17:04 
Generaljust get string, don't save to file. Pin
marco.song10-May-03 9:07
membermarco.song10-May-03 9:07 
GeneralRe: just get string, don't save to file. Pin
Mike Melnikov12-May-03 0:03
memberMike Melnikov12-May-03 0:03 
GeneralRe: just get string, don't save to file. Pin
marco.song13-May-03 16:56
membermarco.song13-May-03 16:56 
GeneralProblem after fixing the memory leaks Pin
grebulon3-Apr-03 6:51
membergrebulon3-Apr-03 6:51 
GeneralQuestion about progam flow Pin
Pankaj Kumar7-Jan-03 20:51
memberPankaj Kumar7-Jan-03 20:51 
Questionwhat about header? Pin
gok19-Dec-02 14:40
membergok19-Dec-02 14:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Cookies | Terms of Use | Mobile
Web04 | 2.8.190114.1 | Last Updated 29 Aug 2001
Article Copyright 2001 by Mike Melnikov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid