首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RapidXML属性值错误

RapidXML属性值错误
EN

Stack Overflow用户
提问于 2014-01-07 11:16:32
回答 1查看 758关注 0票数 0

我以前使用过RapidXML,没有几个问题,但这个问题让我很困惑。

我正在创建一个应用程序事件时间戳的日志,外部程序可以在正确的时间重播在原始应用程序中发生的任何音频。

在初始化应用程序时,正确生成以下XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<playbacklog>
    <logitem type="general" event="start" timestamp="85639323"/>
</playbacklog>

一旦添加了下一项,文档就变成如下:

代码语言:javascript
复制
<?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>

然后:

代码语言:javascript
复制
<?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>

通过添加每个新的startstop对,还可以看到最后的行为,对于具有相同事件属性值的所有节点,时间戳值将被更改:

代码语言:javascript
复制
<?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++头文件中将文档声明为这样:

代码语言:javascript
复制
private:
    rapidxml::xml_document<> outputDocument;

为了创建每个节点,我使用以下代码:

代码语言:javascript
复制
// 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);
}

时间戳*值传递给该函数,如std::vector中所保存的那样,并且当添加一个新函数时,将调用该函数。

如果有人对这里正在发生的事情有任何想法,这将是一个巨大的帮助。此外,如果需要更多的信息,我也将能够提供这一点。

EN

回答 1

Stack Overflow用户

发布于 2014-01-10 10:20:52

这是一个经典的RapidXML‘抓住你’。每当您将char指针传递给RapidXML时,它只是存储指针,而不是复制字符串。这是明确的文件,但仍然经常抓到人。tree

答案是像这样使用allocate_string函数:

代码语言:javascript
复制
 nodeToAdd->append_attribute(outputDocument.allocate_attribute("timestamp", 
                             outputDocument.allocate_string(ts->first.c_str()))); 

(您不需要在allocate_string中包装“时间戳”,因为这是一个文字,所以不会更改)。

我通常使用我自己的辅助包装器-类似这样的东西:-

代码语言:javascript
复制
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()));
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20970205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档