问题
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