RapidXML属性值错误
RapidXML attribute value errors
我过去使用过RapidXML,遇到过一些问题,但这次遇到了麻烦。
我正在创建一个应用程序事件时间戳的日志,外部程序可以在回放中读取在正确时间出现在原始应用程序中的任何音频。
初始化应用程序时,将正确生成以下XML:
<?xml version="1.0" encoding="utf-8"?>
<playbacklog>
<logitem type="general" event="start" timestamp="85639323"/>
</playbacklog>
一旦添加了下一个项目,文档就会变成这样:
<?xml version="1.0" encoding="utf-8"?>
<playbacklog>
<logitem type="general" event="start" timestamp="NUL NUL NUL NUL"/>
<logitem type="audio" event="start" timestamp="86473833">
</playbacklog>
然后:
<?xml version="1.0" encoding="utf-8"?>
<playbacklog>
<logitem type="general" event="start" timestamp="@NUL NUL' NUL NUL"/>
<logitem type="audio" event="start" timestamp="NUL NUL NUL NUL">
<logitem type="audio" event="stop" timestamp="8654533">
</playbacklog>
随着每个新的开始和停止对的添加,还可以看到以下最终行为,具有相同事件属性值的所有节点的时间戳值都会发生变化:
<?xml version="1.0" encoding="utf-8"?>
<playbacklog>
<logitem type="general" event="start" timestamp="@NUL NUL' NUL NUL"/>
<logitem type="audio" event="start" timestamp="NUL NUL NUL NUL">
<logitem type="audio" event="stop" timestamp="8674519">
<logitem type="audio" event="start" timestamp="NUL NUL NUL NUL">
<logitem type="audio" event="stop" timestamp="8674519">
<logitem type="audio" event="start" timestamp="NUL NUL NUL NUL">
<logitem type="audio" event="stop" timestamp="8674519">
</playbacklog>
我在c++头文件中这样声明文档:
private:
rapidxml::xml_document<> outputDocument;
为了创建每个节点,我使用以下代码:
// tStamp is a typedef'd std::pair containing two std::string values, one for the
// time at which the evet occurred and the other containing the event type.
void AudioLogger::LogEvent( Timestamp* tStamp )
{
rapidxml::xml_node<>* nodeToAdd = outputDocument.allocate_node(rapidxml::node_element, "logitem");
...
nodeToAdd->append_attirbute(outputDocument.allocate_attribute("timestamp", ts->first.c_str()));
...
outputDocument.first_node()->next_sibling()->append_node(nodeToAdd);
}
传递给此函数的TimeStamp*值保存在std::向量中,当添加新值时,将调用此函数。
如果有人对这里发生的事情有任何想法,那将是一个巨大的帮助。此外,如果需要更多信息,我也可以提供。
这是一个经典的RapidXML"gotcha"。每当您将char指针传递给RapidXML时,它只是存储指针,而不是复制字符串。它有明确的记录,但仍然经常引起人们的注意。http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1modifying_dom_tree
答案是这样使用allocate_string
函数:
nodeToAdd->append_attribute(outputDocument.allocate_attribute("timestamp",
outputDocument.allocate_string(ts->first.c_str())));
(您不需要在allocate_string中包装"timestamp",因为这是一个文字,因此不会更改)。
我通常使用我自己的辅助包装器-类似这样的东西:-
class MyRapidXmlDoc : public rapidxml::xml_document<char>
{
...
Attribute* allocateAttribute(const string &name, const string &value = "")
{
if (value.empty())
return allocate_attribute(allocate_string(name.c_str()));
else
return allocate_attribute(allocate_string(name.c_str()), allocate_string(value.c_str()));
}
相关文章:
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- CMake 错误 - 目标 foo INTERFACE_SOURCES属性包含在源目录中以前缀为前缀的路径
- 当警告为错误时使用 [[已弃用]] 属性 (-Werror)
- C++ 中常量属性的初始化构造函数错误
- GCC 编译器是否应该对涉及 [[fallthrough]] 属性的格式错误的C++代码进行诊断?
- 我想获取点的属性,它报告错误 C3867:"point::output_x":非标准语法;使用"&"创建指向成员的指针
- MFC 对话框属性表用法在 VS 2017 中产生错误,适用于 VS 2013
- pybind11:属性错误:尝试从 py 文件运行函数时,模块'XXX'没有属性'YYY'
- 创建新对象并立即为其设置属性时出现编译器错误
- Windows 脚本主机 - 错误 '800A01B6' 对象不支持此属性或方法
- accelerator.cu(8): 错误:属性"managed"在这里不适用?
- 我刚刚安装了Visual Studio,遇到了一些错误,特别是WindowsSDKDir属性未定义.可能找不到某些生成工
- 有没有办法将 vector<unsigned char> 插入 postgresql 表中,具有 bytea 属性,没有 UTF8 编码错误?
- LNK2019与另一个工作项目具有相同属性的项目上的错误
- Arduino / esp8266:错误:'Pchr 不允许部分属性
- TensorFlow C调整更改属性的大小错误
- 错误 - 无需匹配功能,将类别为属性C 呼叫
- 编译错误提升属性树C
- binary_search on属性的汇编错误
- X11 XGetImage badmatch错误(无效的参数属性)