从std :: string中删除所有xml标记

时间:2022-08-27 16:29:32

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)

我有一个std :: string xmlString =“ Hello ”,我想删除所有标签! (没有额外的库,除了tinyXML - >已经加载)

result -> Hello

结果 - >你好

Thx

3 个解决方案

#1


3  

If your compiler and standard library support the new C++11 regular expressions you might be able to use std::regex_replace.

如果您的编译器和标准库支持新的C ++ 11正则表达式,那么您可以使用std :: regex_replace。

There are also other regular expression libraries you could use.

您还可以使用其他正则表达式库。

If you don't want to use regular expressions, then you could manually copy the string, while checking for "tags". When you see a '<' just continue looping without copying until you see a '>'.

如果您不想使用正则表达式,那么您可以手动复制字符串,同时检查“标签”。当你看到'<'时,只需继续循环而不复制,直到你看到'>'。

#2


3  

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;
}

#3


0  

If you already use tinyXML, iterate over all nodes depth-first and append the text of the node to the string you're building. There are some answers of SO on how to do that, i.e. TinyXML Iterating over a Subtree

如果您已经使用了tinyXML,则深度优先遍历所有节点,并将节点文本附加到您正在构建的字符串中。关于如何做到这一点有一些SO的答案,即TinyXML迭代子树

#1


3  

If your compiler and standard library support the new C++11 regular expressions you might be able to use std::regex_replace.

如果您的编译器和标准库支持新的C ++ 11正则表达式,那么您可以使用std :: regex_replace。

There are also other regular expression libraries you could use.

您还可以使用其他正则表达式库。

If you don't want to use regular expressions, then you could manually copy the string, while checking for "tags". When you see a '<' just continue looping without copying until you see a '>'.

如果您不想使用正则表达式,那么您可以手动复制字符串,同时检查“标签”。当你看到'<'时,只需继续循环而不复制,直到你看到'>'。

#2


3  

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;
}

#3


0  

If you already use tinyXML, iterate over all nodes depth-first and append the text of the node to the string you're building. There are some answers of SO on how to do that, i.e. TinyXML Iterating over a Subtree

如果您已经使用了tinyXML,则深度优先遍历所有节点,并将节点文本附加到您正在构建的字符串中。关于如何做到这一点有一些SO的答案,即TinyXML迭代子树