tinyXml如何添加元素

发布于 2024-11-07 04:34:38 字数 582 浏览 6 评论 0原文

我有以下内容:

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>

THX!

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

I have the following:

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 ); 

IT IS OK LIKE THIS? I WOULD LIKE TO HAVE the .xml like:

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

THX!

my question is if i can have a int value as a string. if it;s ok if i send in that way the xml file? or is there a way to specify that 5 is an int and not a text?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-11-14 04:34:38

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

If you want to append a node containing an integer value, this integer has first be transformed to a string. You can do this with a variety of functions, but I prefer snprintf (others might differ :) )

Consider the following example:

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 ); 
时间海 2024-11-14 04:34:38

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

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

As the name suggests, a TiXmlText node is text. You can send a textual representation of an integer, but you can't treat the node's value as an integer, unless you convert it yourself.

In summary, it's up to you to convert from whatever type to text when you store it in the TiXmlText node, and then back from text to whatever type when you retrieve it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文