Click here to Skip to main content
12,552,693 members (55,862 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked

XML* (or XMLStar), a C++ STL Based XML Serialization Engine

, 6 May 2013 LGPL3
Rate this:
Please Sign up or sign in to vote.
An STL based simple XML serialization and de-serialization engine


This package is intended to provide the user with a simple to use XML serialization engine. I was originally going to call it XMLite but that name was already taken by Microsoft. The intent is to have a lightweight system. It handles basic XML elements, attributes, comments and processes. More complicated syntax is not supported. For example, it can read a Microsoft Word generated XML file but it can’t save that text file back to XML for Microsoft to use because Microsoft embeds BLOBs as attributes for graphics objects in the Word document. However for simple XML, it works fine. It has also been used on some fairly large files for testing. XMLStar is LGPL licensed. The source code can also be found here.

1 Reading in XMLStar

This is done by creating a root node or top XMLElement and an XMLReader class.

Std::string strXMLInput //XML string data inputted into this string first via assignment or streaming in.

XMLElementIndex objRootIndex;
XMLElement objTopNode;
XMLReader objReader;
//Loading an XML is done by this
/*LoadXMLTree(XMLElement * ptrCurrTop,const
std::string & strInput, XMLElementIndex & objCurrElementIndex)*/
objReader.LoadXMLTree( &objTopNode,strXMLInput,objRootIndex);

The XML input string is then parsed by the reader and the full XML object tree is created with the objTopNode as the root. There are overloads for using string streams and file streams as well.

//!Expand the XML
string value of the element
virtual int LoadXMLTree(XMLElement * ptrCurrTop, const std::string & strInput, XMLElementIndex
& objCurrElementIndex);
virtual int LoadXMLTree(XMLElement * ptrCurrTop, std::istream
& strStreamInput);
virtual int LoadXMLTree(XMLElement * ptrCurrTop,
std::ifstream & strStreamInput);

2 Writing in XMLStar

Writing in XMLStar is similar to reading. The user takes their built XML tree and feeds the top element and the target output string into the XMLWriter.

std::string strXMLOutput; 
XMLElement objTopNode;
//build your XML tree before exporting to text
XMLWriter objWriter;
//loading an XML is done by this
/*SaveXMLTree(XMLElement * ptrCurrTop,
std::string & strOutput, bool blnWithFormatting);*/
objWriter.SaveXMLTree(&objTopNode, strXMLOutput, true);

These are the various overloads for the Save function.

//!Save the entire XML
Tree to desired string output
virtual int SaveXMLTree(XMLElement * ptrCurrTop, std::string
& strOutput, bool blnWithFormatting);
virtual int SaveXMLTree(XMLElement * ptrCurrTop, std::ostream
& strStreamOut, bool blnWithFormatting);
virtual int SaveXMLTree(XMLElement * ptrCurrTop,
std::ofstream & strStreamOut, bool

3 XMLStar Class Architecture

XMLStar is comprised of eight classes, six classes that make up the model tree and two classes for reading and writing XML text. XML documents are inherently tree node style data architectures. The base class of the model tree is the XMLNode. XMLAttribute, XMLComment, XMLProcess, and XMLElement inherit from XMLNode and make up the four primary node types. XMLDocument inherits from XMLElement and makes up the last model node type.

3.1 XMLNode

XMLNode is the base node type. It has the following member variables. Essentially, the node has a name and value pair associated with it along with typing information for the value being stored.

//!Set and Get for Node Type
void Set_enumNodeType(XMLNodeType objNodeType);
XMLNodeType Get_enumNodeType(void) const;
//!Set and Get for Parent Node
void Set_ptrParentNode(XMLNode * ptrParent);
XMLNode *  Get_ptrParentNode(void) const;
//!Set and Get for Parent Node
void Set_objIndex(XMLNodeIndex objIndex);
void Set_objIndex(int intLevel, int intRow, int intColumn);
XMLNodeIndex Get_objIndex(void) const;
//!Set and Get for Level
void Set_intLevel(int intLevel);
int Get_intLevel(void) const;
//!Set and Get for Row 
void Set_intRow(int intRow);
int Get_intRow(void) const;
//!Set and Get for Column
void Set_intColumn(int intCol);
int Get_intColumn(void) const;
//!Set and Get for Node Name string
void Set_strName(const std::string & strName);
void Set_strName(const char* ptrCharString);
std::string Get_strName(void) const;
//!Set and Get for Node Value string
//This function has overloads for the different types of values input
//such as bool, short, int, long, float, double, etc.
void Set_strValue(const std::string & strValue);
std::string Get_strValue(void);
//!Set and Get for the Node value type enumeration XML_BOOL, XML_INT, XML_FLOAT etc.
void Set_enumValueType(XMLValueType enumType);
XMLValueType Get_enumValueType(void) const;

3.2 XMLElement

XMLElement is the primary node of use for storing information. An element can own a collection of Attributes, Comments, Processes, and Sub Elements. There are container manipulation methods for each of the four composite collections previously mentioned. Here is a sample of the methods available for a collection.

ACCESSOR FUNCTIONS/////////////////////
//!Add an attribute to the element
XMLAttribute * AddAttribute(void);
//!Add an attribute to the element
int AddAttribute(XMLAttribute * ptrAttrib);
//!Delete an attribute from the element by its name
int DeleteAttribute(const std::string & strName);
//!Delete an attribute from the element by its index
int DeleteAttribute(size_t lngIndex);
//!Delete All of the Element's Attributes
int DeleteAllAttributes(void);
//!Sort the collection of Attributes Alphabetically
int SortAttributes(void);
//!Size of the Attribute collection
long CountAttributes(void);
//!Boolean check to see if attribute collection is empty
bool HasAttributes(void);
//!Does Attribute Exist check by it's name
bool DoesAttributeExist(const std::string & strName);
//!Get the pointer to the first Attribute
XMLAttribute * FirstAttribute(void);
//!Get the pointer to the last Attribute
XMLAttribute * LastAttribute(void);
//!Get the pointer to the next Attribute
XMLAttribute * NextAttribute(void);
//!Get the pointer to the previous Attribute
XMLAttribute * PreviousAttribute(void);
//!Get the pointer to the Attribute by it's name
XMLAttribute * GetAttribute(const std::string & strName);
//!Get the pointer to the Attribute by it's index
XMLAttribute * GetAttribute(size_t lngIndex);
//!Get the pointer to the Attribute by it's index
XMLAttribute * AtAttribute(size_t lngIndex);

These kind of collection functions are available for Attributes, Comments, Processes, and Sub Elements. In addition to this, there are methods for updating indexes and other data members at the element level.

//!Sets the null node
//!Sets the root node flag
void Set_blnRootNode(bool blnIsRootNode);
//!Gets the root node flag value
bool Get_blnRootNode(void) const;
//!Indicates whether or not this element is the root node
bool IsRootNode(void);

//!Sets the null node flag
void Set_blnNullNode(bool blnIsNullNode);
//!Gets the null node flag value
bool Get_blnNullNode(void) const;
//!Indicates whether or not this element is the null node
bool IsNullNode(void);
//!Update the indexes
int UpdateIndexes(void);

3.3 XMLAttribute, XMLComment

Both XMLAttribute and XMLComment node types have no additional information than their node name and node value.

3.4 XMLProcess

XMLProcess has a collection of Attribute nodes that belong to it. The same accessor functions are available as in the XMLElement for these attributes. XML processes are considered to be code blocks of interpreted scripts such as Java.

3.5 XMLDocument

XMLDocument is essentially an XMLElement with additional document properties for the prologue tags.

//Set and Get for Include Prologue
void Set_blnIncludePrologue(bool blnIncludePrologue);
bool Get_blnIncludePrologue(void) const;

//Set and Get for Document XML Version Number
void Set_strVersion(std::string strVersion);
std::string Get_strVersion(void) const;

//Set and Get for Document Encoding 
void Set_strEncoding(std::string strEncoding);
std::string Get_strEncoding(void) const;

//Set and Get for Include Standalone
void Set_blnStandalone(bool blnStandalone);
bool Get_blnStandalone(void) const;

//Set and Get for Include Document Type Statement
void Set_blnIncludeDocType(bool m_blnIncludeDocType);
bool Get_blnIncludeDocType(void) const;

//Set and Get for Document Type Statement 
void Set_strDocType(std::string strDocType);
std::string Get_strDocType(void) const;


  • 29th November, 2014: Initial version
  • 30th November, 2014: A couple of minor bug fixes in the XMLReader class. The library has been used heavily and is at the V1.0 level of readiness.

Author: Anthony S. Daniels


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionThis was a tip... Pin
OriginalGriff29-Nov-14 19:38
protectorOriginalGriff29-Nov-14 19:38 
GeneralMy vote of 5 Pin
trident9917-Mar-13 14:40
membertrident9917-Mar-13 14:40 
QuestionRe: My vote of 5 Pin
N-O-R-B-E-R-T6-May-13 20:17
memberN-O-R-B-E-R-T6-May-13 20:17 
AnswerRe: My vote of 5 Pin
trident997-May-13 4:40
membertrident997-May-13 4:40 
GeneralRe: My vote of 5 Pin
Nelek29-Nov-14 12:59
protectorNelek29-Nov-14 12:59 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 6 May 2013
Article Copyright 2013 by trident99
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid