我有以下内容:
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/