tinyXml how to add an element

痴心易碎 提交于 2019-12-11 16:54:49

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/6017672/tinyxml-how-to-add-an-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!