简体   繁体   English

Rapidxml正在编写错误的字符

[英]Rapidxml is writing wrong characters

I've been using Rapidxml lately and have faced following problem. 我最近一直在使用Rapidxml,并遇到了以下问题。 When I try to add attributes, which are not hardcoded, but generated during program runtime rapidxml inserts wrong characters. 当我尝试添加非硬编码但在程序运行时生成的属性时,rapidxml会插入错误的字符。

Here is my sample of code: 这是我的代码示例:

   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? 有人可以帮帮我吗?

When you pass a string to RapidXML, it doesn't copy it, it just remembers the address. 当您将字符串传递给RapidXML时,它不会复制它,它只会记住该地址。 So the 'variable' strings you add will get overwritten, hence corrupting the RapidXML document. 因此,您添加的“变量”字符串将被覆盖,从而破坏RapidXML文档。

Read this section here. 在这里阅读本节。

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

In particular, you need to change the allocate_attribute calls that use variables like this:- 特别是,您需要更改使用如下变量的allocate_attribute调用: -

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

This question of mine may be relevant, too: How to fix RapidXML String ownership concerns? 我的这个问题也可能是相关的: 如何修复RapidXML字符串所有权问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM