Introduction
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;
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.
DE-SERIALIZATION METHODSstring 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;
XMLWriter objWriter;
objWriter.SaveXMLTree(&objTopNode, strXMLOutput, true);
These are the various overloads for the Save
function.
DE-SERIALIZATION METHODSTree 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
blnWithFormatting);
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.
FUNCTIONSvoid Set_enumNodeType(XMLNodeType objNodeType);
XMLNodeType Get_enumNodeType(void) const;
void Set_ptrParentNode(XMLNode * ptrParent);
XMLNode * Get_ptrParentNode(void) const;
void Set_objIndex(XMLNodeIndex objIndex);
void Set_objIndex(int intLevel, int intRow, int intColumn);
XMLNodeIndex Get_objIndex(void) const;
void Set_intLevel(int intLevel);
int Get_intLevel(void) const;
void Set_intRow(int intRow);
int Get_intRow(void) const;
void Set_intColumn(int intCol);
int Get_intColumn(void) const;
void Set_strName(const std::string & strName);
void Set_strName(const char* ptrCharString);
std::string Get_strName(void) const;
void Set_strValue(const std::string & strValue);
std::string Get_strValue(void);
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 FUNCTIONSXMLAttribute * AddAttribute(void);
int AddAttribute(XMLAttribute * ptrAttrib);
int DeleteAttribute(const std::string & strName);
int DeleteAttribute(size_t lngIndex);
int DeleteAllAttributes(void);
int SortAttributes(void);
long CountAttributes(void);
bool HasAttributes(void);
bool DoesAttributeExist(const std::string & strName);
XMLAttribute * FirstAttribute(void);
XMLAttribute * LastAttribute(void);
XMLAttribute * NextAttribute(void);
XMLAttribute * PreviousAttribute(void);
XMLAttribute * GetAttribute(const std::string & strName);
XMLAttribute * GetAttribute(size_t lngIndex);
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.
void Set_blnRootNode(bool blnIsRootNode);
bool Get_blnRootNode(void) const;
bool IsRootNode(void);
void Set_blnNullNode(bool blnIsNullNode);
bool Get_blnNullNode(void) const;
bool IsNullNode(void);
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.
void Set_blnIncludePrologue(bool blnIncludePrologue);
bool Get_blnIncludePrologue(void) const;
void Set_strVersion(std::string strVersion);
std::string Get_strVersion(void) const;
void Set_strEncoding(std::string strEncoding);
std::string Get_strEncoding(void) const;
void Set_blnStandalone(bool blnStandalone);
bool Get_blnStandalone(void) const;
void Set_blnIncludeDocType(bool m_blnIncludeDocType);
bool Get_blnIncludeDocType(void) const;
void Set_strDocType(std::string strDocType);
std::string Get_strDocType(void) const;
History
- 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
AnthonyDaniels99@gmail.com