title: Need assistance with C++ function to parse/display XML serialization
body: <p>I need some help getting my XML serialization to work properly. Right now I have two functions that read my XML file, char by char. Right now they successfully detect and store elements as well as storing the content of the elements as a variable. I have the function print the hierarchy of the elements and they successfully show the contents that they contain.</p>
  
  <p>My Problem: I can't get the function to correctly identify an end tag for the elements! For example, when the parsing gets to the elements end tag, it detects it as an element rather than the end of an element. </p>
  
  <p><strong>Sorry for all the text and the length of the code. I'm trying to be as thorough as possible. Thanks in advanced!</strong></p>
  
  <p>This is what is displayed when the code is compiled and ran. Notice hot there are elements that equal nothing:</p>
  
  <pre><code>XML.World.Item.name = silver key
  XML.World.Item.properties.property = metal
  XML.World.Item.properties.property = silver
  XML.World.Item.properties = 
  XML.World.Item.weight = 1
  XML.World.Item.displayChar = )
  XML.World.Item.value = 10
  XML.World.Item.rarity = 5
  XML.World.Item = 
  XML.World.Creature.name = orc
  XML.World.Creature.properties.property = orcish
  XML.World.Creature.properties.property = humanoid
  XML.World.Creature.properties = 
  XML.World.Creature.level = 2
  XML.World.Creature.maxHP = 15
  XML.World.Creature.displayChar = o
  XML.World.Creature = 
  XML.World = 
  </code></pre>
  
  <hr>
  
  <p><strong>My XML file is called world.xml and this is the XML code it contains:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;World&gt;
  &lt;Item&gt;
  &lt;name&gt;silver key&lt;/name&gt;
  &lt;properties&gt;
  &lt;property&gt;metal&lt;/property&gt;
  &lt;property&gt;silver&lt;/property&gt;
  &lt;/properties&gt;
  &lt;weight&gt;1&lt;/weight&gt;
  &lt;displayChar&gt;)&lt;/displayChar&gt;
  &lt;value&gt;10&lt;/value&gt;
  &lt;rarity&gt;5&lt;/rarity&gt;
  &lt;/Item&gt;
  &lt;Creature&gt;
  &lt;name&gt;orc&lt;/name&gt;
  &lt;properties&gt;
  &lt;property&gt;orcish&lt;/property&gt;
  &lt;property&gt;humanoid&lt;/property&gt;
  &lt;/properties&gt;
  &lt;level&gt;2&lt;/level&gt;
  &lt;maxHP&gt;15&lt;/maxHP&gt;
  &lt;displayChar&gt;o&lt;/displayChar&gt;
  &lt;/Creature&gt;
  &lt;/World&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Here is my code being used - XMLSerialization.h, XMLSerialization.cpp, and main.cpp:</strong></p>
  
  <p><strong>XMLSerialization.h</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  
  class XMLSerialization {
  
  public:
      virtual bool parseElement(std::istream &amp; xmlFile, std::string hierarchy$
      virtual bool parseXML(std::istream &amp; xmlFile);
  
  private:
      //none
  };
  </code></pre>
  
  <p><strong>XMLSerialization.cpp</strong></p>
  
  <pre><code>#include "XMLSerialization.h"
  #include &lt;string&gt;
  
  using namespace std;
  bool XMLSerialization::parseElement(istream &amp; xmlFile, string hierarchy){
      char c; // the character as we reach it
      string elementName;
  
      //reads char by char, checking for '&gt;' at the end of the tag
  
      do {
          c = xmlFile.get();
          if (c != '&gt;')
              elementName.push_back(c);
      }
      while (c != '&gt;');
  
      string content; //holds the non-element content of the element
  
      while (true){
              c = xmlFile.get();
              if (c == '&lt;'){
                      if (xmlFile.peek() == '/'){
                              xmlFile.get();
                              string endTag; //holds the end tag as its read
  
                              while(c != '&gt;'){
                                      c = xmlFile.get();
                                      if (c != '&gt;'){
                                              endTag.push_back(c);
                                      }
                              }
  
                              if (endTag != elementName){
                                  cout&lt;&lt;"Tag name mismatch! "&lt;&lt;endTag&lt;&lt;
                                              " differs from "&lt;&lt;elementName
                                              &lt;&lt;"."&lt;&lt;endl;
                                      return false;
                              }
  
                              //output what is known. Where we are in the
                              //file, current element, and its content
                              cout&lt;&lt;hierarchy&lt;&lt;"."&lt;&lt;elementName&lt;&lt;" = "&lt;&lt;
                                      content&lt;&lt;endl;
                              return true;
                      }
                      else {
                              //read in '&lt;' and was NOT an end tag. c is at
                              //the first char after &lt; so function calls
                              //on itself again. Passing hierarchy and current
                              //element name so next element knows where it
                              //is in the xmlFile.
                              if (!parseElement(xmlFile, hierarchy + "." + el$
                                      return false;
                              }
                      }
              }
  
              else {
                      //c is not '&lt;' so its content. Also ignores EOL
                      if (c != '\n'){
                                  content.push_back(c);
                      }
              }
      }
  
      return true;
  }
  
  
  
  // checks for a valid XML Header
  bool XMLSerialization::parseXML(istream &amp; xmlFile){
      char c; // char to hold the character as we reach it
  
      //get character while the character != '&lt;'
      do {
              c = xmlFile.get();
      }
      while (c != '&lt;');
  
      //checks the character after the '&lt;'
      if (xmlFile.get() != '?'){
              cout&lt;&lt;"Invalid XML Header! Does not begin with '&lt;?'"&lt;&lt;endl;
              return false;
      }
     //continues through header look for '?'
      do {
              c = xmlFile.get();
      }
      while (c != '?');
  
      // checks for the header ending with ?&gt;
      if (xmlFile.get() != '&gt;'){
              cout&lt;&lt;"Invalid XML Header! Does not end with '?&gt;'"&lt;&lt;endl;
              return false;
      }
  
      // go through character until the first tag after the header
      do {
              c = xmlFile.get();
      }
      while (c != '&lt;');
  
      // at the first character after the opening '&lt;' of the tag
      // call parseElement
      return parseElement(xmlFile, "XML");
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include "XMLSerialization.h"
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  int main(int argc, char * argv[]){
      cout&lt;&lt;"________________________"&lt;&lt;endl;
      cout&lt;&lt;"XML TESTING"&lt;&lt;endl&lt;&lt;"________________________"&lt;&lt;endl&lt;&lt;endl;
      ifstream xmlFile;
      xmlFile.open("world.xml");
      XMLSerialization test;
      test.parseXML(xmlFile);
      xmlFile.close();
      return 0;
  }
  </code></pre>