Xml object – c++

From the main project xmlreader, here is the xml object that I created to have a xml line of code which holds the details of a basic xml.

The main basics of a xml are as below

<tagname attributesname="attributesvalue">value</tagname>

So I will need to store the tagname, attributes and the value. Here is the class structure that I did come up with.

class xmlObject {
  private :
    string _tagName, _tagValue;
    vector<xmlAttribute> _attributes;
    bool _xmlMainDetails;
      xmlObject() { _xmlMainDetails = false;} ;
      xmlObject(string tag, string tValue, xmlAttribute attribute);
      void setTagName(string tagName);
      void setTagValue(string tagValue);
      void addAttributes(xmlAttribute attribute);
      void setAttributeVector(vector<xmlAttribute> setAtt);
      void setXmlMainDetails(bool value);
      bool getXmlMainDetails();
      void printOutXmlObject();

The set/getXmlMainDetails are if the are at the top of the xml file and need to store them in a different place.

A vector is a nice array basically, it allows to dynamically increment the size of the array with using the push_back (and the opposite to shrink pop_back).

The basics of a vector are as below, means to have a vector of type int

vector<int> intvector;

Here is the class implementation of the object structure xmlObject

/* xmlObject */
// constructor for xmlObject, if any details are passed whilst constructing 
xmlObject::xmlObject(string tag, string tValue, xmlAttribute attribute)
  _tagName = tag;
  _tagValue = tValue;
// xml <tagname attributes="attributesvalue">VALUE</tagname>
// set the tag name
void xmlObject::setTagName(string tagName)
  _tagName = tagName;
// set tag value
void xmlObject::setTagValue(string tagValue)
  _tagValue = tagValue;
// add attributes to the vector attributes variable
void xmlObject::addAttributes(xmlAttribute attribute)
// fill in the vector attributes variable.
void xmlObject::setAttributeVector(vector<xmlAttribute> setAtt)
  _attributes = setAtt;
// print out the xml object detais, with the attributes values.
void xmlObject::printOutXmlObject()
  cout << "XML Object" << endl;
  cout << "Tagname  :" << _tagName << endl;
  cout << "Tagvalue :" << _tagValue << endl;
  for (int i= 0; i < (int)_attributes.size(); i++)
      cout << "Attribute " << i << " : Name : "<< _attributes.at(i)._attributeName << " Value : " << _attributes.at(i)._attributeValue << endl;
// set the main set details value
void xmlObject::setXmlMainDetails(bool value)
    _xmlMainDetails = value;
// get a boolean value to see if the xmlObject is the main <?xml .. ?> value
bool xmlObject::getXmlMainDetails()
  return _xmlMainDetails;

I shall post on how to implement/compile etc a class in two different files later on, in a lessons basics for different languages but on the whole, if you store the top structure in a .h header file and then the implementation in a .cpp file. Of course shall post the whole code to store in .h .cpp files accordlying for the whole project but this is just a stripped down version.

Leave a Reply

Your email address will not be published. Required fields are marked *