Opening and validating an XML file I have a section of code that repeats many lines and I am looking to refactor this code possibly so that a function can be used instead of the same lines of code each time. Below is the section of code and the repeating code occurs in each if statement

Traversal::Traversal(string source, bool isFileName)
 {
      using namespace XML_Parser;
      using std::endl;

      std::ostringstream ppd;
  if (isFileName)
  {
      string content, line;
      std::ifstream file(source);
      if (! file)
      {
          ppd &lt;&lt; endl &lt;&lt; "error opening file " &lt;&lt; source;
          cRep = ppd.str();
          cSuc = false;
          return;
      }
      ppd &lt;&lt; endl &lt;&lt; " file " &lt;&lt; source &lt;&lt; " opened ";
      while (file.good())
      {
          getline(file, line);
          content += '\n';
          content += line;
      }
      ppd &lt;&lt; endl &lt;&lt; "file " &lt;&lt; source &lt;&lt; " read ";
      source = content;
  }
  if (! elementExists(source,"afcd"))
  {
      ppd &lt;&lt; endl &lt;&lt; "no afcd tag";
      cRep = ppd.str();
      cSuc = false;
      return;
  }
  if (! elementExists(source,"rteirl"))
  {
      ppd &lt;&lt; endl &lt;&lt; "no rteirl tag";
      cRep = ppd.str();
      cSuc = false;
      return;
  }


How would I go about making this cleaner?
The basic aim is to put repeating parts in the function, while passing as a parameter anything that's different:

if (elementMissing(source, "afcd")) {
    return;
}

if (elementMissing(source, "rteirl")) {
    return;
}

bool elementMissing(source, element) {
    if (! elementExists(source, element)) {
        ppd &lt;&lt; endl &lt;&lt; "no " &lt;&lt; element &lt;&lt; " tag";
        cRep = ppd.str();
        cSuc = false;
        return true;
    } else {
        return false;
    }
}


I'm not sure what cRep and cSuc are supposed to be doing, so I can't help you with that part.