c++ - tinyXml如何添加一个元素

标签 c++ ubuntu tinyxml

我有以下内容:

TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "utf-8", "");
doc.LinkEndChild( decl );
TiXmlElement * root = new TiXmlElement( "Value" );  
TiXmlElement * element = new TiXmlElement( "number" );  
root->LinkEndChild( element);  

TiXmlText * text = new TiXmlText( "5" );  
element->LinkEndChild( text ); 

这样可以吗?我想要这样的 .xml:

<Value>
<number>5</number>
</Value>

谢谢!

我的问题是我是否可以将 int 值作为字符串。如果可以的话,如果我以这种方式发送 xml 文件可以吗?或者有没有办法指定 5 是一个 int 而不是文本?

最佳答案

如果你想附加一个包含整数值的节点,这个整数首先被转换成一个字符串。您可以使用多种函数来执行此操作,但我更喜欢 snprintf(其他函数可能不同 :))

考虑以下示例:

int five = 5;
char buf[256];
snprintf(buf, sizeof(buf), "%d", five); // transforms the integer to a string
TiXmlText * text = new TiXmlText( buf );  
element->LinkEndChild( text ); 

关于c++ - tinyXml如何添加一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6017672/

相关文章:

c++ - 使用 VS2010 MFC 的简单 DLL + 测试应用程序

c++ - 在哪里可以找到 "Introduction to 3D game programming with DirectX 9.0c"的源代码示例?

linux - Tesseract 无法打开输入文件[Ubuntu]

c++ - TinyXML 抛出访问冲突

c++ - 使用 CWiseNullaryOp 扩展 Eigen : Cannot reproduce example

C++数组选择平方数并制作新 vector

linux - 包 'libpng12-0' 没有安装候选 Ubuntu 18.0.4

google-chrome - NET::ERR_CERT_WEAK_SIGNATURE_ALGORITHM 错误,用于 ubuntu 上的谷歌浏览器稳定版

c++ - 使用 TinyXml for C++ 时需要清理哪些内存管理?