开发者

tinyXml how to add an element

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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜