Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ tinyXml如何添加元素_C++_Ubuntu_Tinyxml - Fatal编程技术网

C++ tinyXml如何添加元素

C++ tinyXml如何添加元素,c++,ubuntu,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->LinkEndCh

我有以下资料:

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>

5.
谢谢


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

如果要附加包含整数值的节点,则必须首先将该整数值转换为字符串。您可以使用多种函数来实现这一点,但我更喜欢
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 ); 

顾名思义,
TiXmlText
节点就是文本。可以发送整数的文本表示形式,但不能将节点的值视为整数,除非您自己进行转换


总之,在
TiXmlText
节点中存储文本时,您可以将其从任何类型转换为文本,然后在检索文本时将其从文本转换为任何类型。

是否尝试运行代码?您是否遇到过任何特定问题?您没有对“root”变量执行任何操作。我的问题是是否可以将int值作为字符串。若有,;如果我以这种方式发送xml文件,那还可以吗?或者有没有办法指定5是整数而不是文本?老实说,我不明白你的问题。XML文件只是一个文本文件,一个字符序列。xml文件中没有整数。可能与I see.Thx重复。TiXmlText()是否在括号内表示字符串值?是的。它需要一个
char
std::string