gpt4 book ai didi

c++ - RapidXML 奇怪的解析

转载 作者:太空狗 更新时间:2023-10-29 12:35:54 31 4
gpt4 key购买 nike

我有一个非常恼人的问题,我花了很多时间来解决它。我在 C++ 中使用 rapidXML 来解析 XML 文件:

xml_document<> xmlin;
stringstream input; //initialized somewhere else
xmlin.clear();
xmlin.parse<0>(&(input.str()[0]));

cout << "input:" << input.str() << endl << endl;

xml_node<char> *firstnode = xmlin.first_node();
string s_type = firstnode->first_attribute("type")->value();
cout << "type: " << s_type << endl;

但是我在标准输出上得到了这个:

input:<?xml version="1.0" encoding="utf-8"?><testxml command="testfunction" type="exclusive" />

type: exclusive" />

这可能是什么原因(打印 s_type 变量)?这很烦人,因为我无法很好地处理 xml。

最佳答案

实际上我找到了解决方案。

Stringstream 不喜欢它的内容被修改(rapidXML 进行快速的原位解析,这意味着它修改它获取的数组的内容)。

但是在我读到的文档中,字符串类也不喜欢它。

来自 string::c_str 文档页面:

the values in this array should not be modified in the program

但是当我从流中创建一个字符串时,它按预期工作:

xml_document<> xmlin;
stringstream input; //initialized somewhere else
string buffer = input.str()

xmlin.clear();
xmlin.parse<0>(&(buffer[0]));

关于c++ - RapidXML 奇怪的解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11768514/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com