Search code examples
c++xmlstringtinyxml

Remove all xml tags from a std::string


I have a std::string xmlString = "<out><return>Hello</return></out>" and I want to remove all of the tags! (without an additional library, except tinyXML -> already loaded)

result -> Hello

Thx


Solution

  • Possible solution:

    std::string ClassA::ParseXMLOutput(std::string &xmlBuffer)
    {
        bool copy = true;
        std::string plainString = "";   
        std::stringstream convertStream;
    
        // remove all xml tags
        for (int i=0; i < xmlBuffer.length(); i++)
        {                   
            convertStream << xmlBuffer[i];
    
            if(convertStream.str().compare("<") == 0) copy = false;
            else if(convertStream.str().compare(">") == 0) 
            {
                copy = true;
                convertStream.str(std::string());
                continue;
            }
    
            if(copy) plainString.append(convertStream.str());       
    
            convertStream.str(std::string());
        }
    
        return plainString;
    }