Rapidxml写错字符 [英] Rapidxml is writing wrong characters

查看:105
本文介绍了Rapidxml写错字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用Rapidxml,并且遇到了以下问题.当我尝试添加未进行硬编码但在程序运行时生成的属性时,rapidxml会插入错误的字符.

I've been using Rapidxml lately and have faced following problem. When I try to add attributes, which are not hardcoded, but generated during program runtime rapidxml inserts wrong characters.

这是我的代码示例:

   void ProcessInfo::retriveInfo()
{
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

   PROCESSENTRY32 pe = { sizeof(pe) };  
   BOOL fOk = ProcessFirst( &pe, hSnapshot );

   using namespace rapidxml;
   xml_document<> doc;

   xml_node<>* decl = doc.allocate_node(node_declaration);
   decl->append_attribute(doc.allocate_attribute("version", "1.0"));
   decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
   doc.append_node(decl);

   xml_node<>* root = doc.allocate_node(node_element, "rootnode");

   while(fOk)
   {
       std::string processFile = pe.szExeFile;

   xml_node<>* processName = doc.allocate_node(node_element, PROCESS_NODE);
       root->append_node( processName );


       processName->append_attribute(doc.allocate_attribute( PROCESS_NAME, processFile.c_str() ) );

       char szPID[PID_BUFFER];
       memset(szPID, 0x00, sizeof(szPID) );
       itoa(pe.th32ProcessID, szPID, 10 );
       processName->append_attribute(doc.allocate_attribute( PROCESS_ID, szPID ));

       char szParentPID[PID_BUFFER];
       itoa( pe.th32ParentProcessID, szParentPID, 10 );
       processName->append_attribute(doc.allocate_attribute( PROCESS_PARENT_ID, szParentPID ));

       std::cout << processFile.c_str() << " " << szPID <<  " " << szParentPID << std::endl;


       fOk = ProcessNext( &pe, hSnapshot );
   }

   doc.append_node( root );
   std::cout << doc;   

}

编码似乎有些问题,但我无法弄清楚,为什么?有人可以帮我吗?

It seems like something wrong with encoding, but I cannot figure it out, why? Can someone please help me?

推荐答案

当您将字符串传递给RapidXML时,它不会复制它,它只会记住地址.因此,您添加的可变"字符串将被覆盖,从而破坏RapidXML文档.

When you pass a string to RapidXML, it doesn't copy it, it just remembers the address. So the 'variable' strings you add will get overwritten, hence corrupting the RapidXML document.

在这里阅读本节.

http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1modifying_dom_tree

特别是,您需要更改使用如下变量的allocate_attribute调用:-

In particular, you need to change the allocate_attribute calls that use variables like this:-

char *node_name = doc.allocate_string(szPID);        // Allocate string and copy name into it
processName->append_attribute(doc.allocate_attribute(PROCESS_ID, node_name);  // Set node name to node_name

我的这个问题也可能与此有关:如何解决RapidXML String所有权问题?

This question of mine may be relevant, too: How to fix RapidXML String ownership concerns?

这篇关于Rapidxml写错字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆