0

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?

just me
  • 157
  • 1
  • 8
  • 18
  • 1
    did you try running the code ? did you face any *specific* problem? – Naveen May 16 '11 at 12:53
  • You're not doing anything with the "root" variable. – Jem May 16 '11 at 12:56
  • 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? – just me May 16 '11 at 13:00
  • 1
    Honestly, I can't understand your question. A XML file is just a text file, a sequence of character. There's no integer in a xml file. – Kien Truong May 16 '11 at 13:12
  • possible duplicate of [Create xml using tinyxml](http://stackoverflow.com/questions/6016862/create-xml-using-tinyxml) –  May 16 '11 at 13:22

2 Answers2

2

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 ); 
Constantinius
  • 34,183
  • 8
  • 77
  • 85
0

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.

Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71