#include "XmlReader.h" #include #include #include /* function 21 79 if 30 33 if 37 40 while 50 61 if 53 53 if 55 55 else 56 57 if 57 57 if 59 59 if 66 66 if 71 71 if 73 76 */ //----< return current element string >------------------------------ std::string XmlReader::element() { // find tag - assumes _position points to character after // opening "<" on entry localposition = _position; _tag = extractIdentifier(localposition); // is declaration? std::string decTag = "?xml"; if(_tag.compare("?xml") == 0) { size_t locpos = _xml.find("?>"); return _xml.substr(_position-1,locpos-_position+3); } // is comment? if(_tag.compare("!--") == 0) { size_t locpos = _xml.find("-->"); return _xml.substr(_position-1,locpos-_position+4); } // find end of element size_t locpos1 = localposition; // note: tracks element scope with tagStack, so will correctly // return nested elements with the same tag name std::stack tagStack; tagStack.push(_tag); while(true) { locpos1 = _xml.find(_tag,locpos1); if(locpos1 >= _xml.size()) break; if(_xml[locpos1-1]=='/' && _xml[locpos1 + _tag.size()] == '>') // modified 2/15/2012 tagStack.pop(); else if(_xml[locpos1-1]=='<') tagStack.push(_tag); if(tagStack.size() == 0) break; ++locpos1; } // find end element of self-closing tag, e.g., size_t locpos2 = _xml.find(">",localposition); if(_xml[locpos2-1] != '/') locpos2 = _xml.size(); // find end element localposition = std::min(locpos1,locpos2); if(localposition >= _xml.size()) throw std::exception("malformed XML"); if(localposition == locpos1) { localposition = _xml.find('>',localposition); return _xml.substr(_position-1, localposition - _position +2); } return _xml.substr(_position-1, localposition - _position + 2); }