{"id":540,"date":"2010-01-21T11:29:23","date_gmt":"2010-01-21T11:29:23","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=540"},"modified":"2010-01-21T11:29:46","modified_gmt":"2010-01-21T11:29:46","slug":"xml-object-cpp","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/21\/xml-object-cpp\/","title":{"rendered":"Xml object &#8211; c++"},"content":{"rendered":"<p>From the main project <a href=\"http:\/\/www.codingfriends.com\/index.php\/projects\/xml-reader-cpp\/\">xmlreader<\/a>, here is the xml object that I created to have a xml line of code which holds the details of a basic <a href=\"http:\/\/en.wikipedia.org\/wiki\/XML\">xml<\/a>.  <\/p>\n<p>The main basics of a xml are as below<\/p>\n<pre lang=\"xml\">\r\n<tagname attributesname=\"attributesvalue\">value<\/tagname>\r\n<\/pre>\n<p>So I will need to store the tagname, attributes and the value.  Here is the class structure that I did come up with.<\/p>\n<pre lang=\"cpp\">\r\nclass xmlObject {\r\n  private :\r\n    string _tagName, _tagValue;\r\n    vector<xmlAttribute> _attributes;\r\n    bool _xmlMainDetails;\r\n    \r\n  public:\r\n      xmlObject() { _xmlMainDetails = false;} ;\r\n      xmlObject(string tag, string tValue, xmlAttribute attribute);\r\n      \r\n      void setTagName(string tagName);\r\n      void setTagValue(string tagValue);\r\n      void addAttributes(xmlAttribute attribute);\r\n      void setAttributeVector(vector<xmlAttribute> setAtt);\r\n      void setXmlMainDetails(bool value);\r\n      bool getXmlMainDetails();\r\n      void printOutXmlObject();\r\n};\r\n<\/pre>\n<p>The set\/getXmlMainDetails are if the <?xml version=\"1.0\"?> are at the top of the xml file and need to store them in a different place.<\/p>\n<p>A <a href=\"http:\/\/en.wikipedia.org\/wiki\/Vector_%28C%2B%2B%29\">vector<\/a> 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).<\/p>\n<p>The basics of a vector are as below, means to have a vector of type <a href=\"http:\/\/en.wikipedia.org\/wiki\/Integer_%28computer_science%29\">int<\/a><\/p>\n<pre lang=\"cpp\">\r\nvector<int> intvector;\r\n<\/pre>\n<p>Here is the class implementation of the object structure xmlObject<\/p>\n<pre lang=\"cpp\">\r\n\/* xmlObject *\/\r\n\/\/ constructor for xmlObject, if any details are passed whilst constructing \r\nxmlObject::xmlObject(string tag, string tValue, xmlAttribute attribute)\r\n{\r\n  _tagName = tag;\r\n  _tagValue = tValue;\r\n  _attributes.push_back(attribute);\r\n}\r\n\r\n\/\/ xml <tagname attributes=\"attributesvalue\">VALUE<\/tagname>\r\n\/\/ set the tag name\r\nvoid xmlObject::setTagName(string tagName)\r\n{\r\n  _tagName = tagName;\r\n}\r\n\r\n\/\/ set tag value\r\nvoid xmlObject::setTagValue(string tagValue)\r\n{\r\n  _tagValue = tagValue;\r\n}\r\n\r\n\/\/ add attributes to the vector attributes variable\r\nvoid xmlObject::addAttributes(xmlAttribute attribute)\r\n{\r\n  _attributes.push_back(attribute);\r\n}\r\n\r\n\/\/ fill in the vector attributes variable.\r\nvoid xmlObject::setAttributeVector(vector<xmlAttribute> setAtt)\r\n{\r\n  _attributes = setAtt;\r\n}\r\n\r\n\/\/ print out the xml object detais, with the attributes values.\r\nvoid xmlObject::printOutXmlObject()\r\n{\r\n  cout << \"XML Object\" << endl;\r\n  cout << \"Tagname  :\" << _tagName << endl;\r\n  cout << \"Tagvalue :\" << _tagValue << endl;\r\n  for (int i= 0; i < (int)_attributes.size(); i++)\r\n  {\r\n      cout << \"Attribute \" << i << \" : Name : \"<< _attributes.at(i)._attributeName << \" Value : \" << _attributes.at(i)._attributeValue << endl;\r\n  }\r\n}\r\n\r\n\/\/ set the main set details value\r\nvoid xmlObject::setXmlMainDetails(bool value)\r\n{\t\r\n    _xmlMainDetails = value;\r\n}\r\n\r\n\/\/ get a boolean value to see if the xmlObject is the main <?xml .. ?> value\r\nbool xmlObject::getXmlMainDetails()\r\n{\r\n  return _xmlMainDetails;\r\n}\r\n<\/pre>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 value So I will need to store the tagname, attributes and the value. Here is the class structure &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/21\/xml-object-cpp\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Xml object &#8211; c++<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[62,60],"class_list":["post-540","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-object","tag-xml"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=540"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/540\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/540\/revisions\/544"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}