std::string识别出xml换行
时间: 2023-10-23 09:12:08 浏览: 46
如果你想在`std::string`中识别出XML换行符,即`\n`和`\r`,你可以使用`std::string::find()`函数来查找并提取每一行的内容。下面是一个示例代码,可以将文本解析为XML格式并提取每一行的内容:
```c++
std::string xml = "<root>\n\t<tag1>\n\t\tvalue1\n\t</tag1>\n\t<tag2>\n\t\tvalue2\n\t</tag2>\n</root>";
std::string line;
std::size_t start_pos = 0;
std::size_t end_pos = 0;
while ((end_pos = xml.find("\n", start_pos)) != std::string::npos) {
line = xml.substr(start_pos, end_pos - start_pos);
// do something with the line, e.g. print it
std::cout << line << std::endl;
start_pos = end_pos + 1;
}
line = xml.substr(start_pos);
// print the last line
std::cout << line << std::endl;
```
这段代码会输出以下内容:
```
<root>
<tag1>
value1
</tag1>
<tag2>
value2
</tag2>
</root>
```
你可以根据自己的需要修改代码,比如将每一行的内容存储到一个数组或容器中,或者对每一行进行进一步处理。