Link to home
Start Free TrialLog in
Avatar of camster123
camster123

asked on

How to use TinyXml C++ to output the xml for integer values as an integer (without quote).

I used the following TinyXml code

TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild( decl );

TiXmlElement* element = new TiXmlElement( "Hello" );
doc.LinkEndChild( element );

TiXmlText* text = new TiXmlText( "Opening a new salutation" );
element->LinkEndChild( text );


for(long i=0; i<3; i++)
{
TiXmlElement* element2 = new TiXmlElement( "Greeting" );
TiXmlText* text2 = new TiXmlText( "" );

element2->SetAttribute("value","5");
element2->SetAttribute("name","me");

element->LinkEndChild( element2 );
element2->LinkEndChild( text2 );
}

doc.SaveFile( "madeByHand2.xml" );


which I read in the following Stack Overflow posT: http://stackoverflow.com/questions/6016862/create-xml-using-tinyxml


which produces the following output:

<?xml version="1.0" ?>
<Hello>Opening a new salutation
    <Greeting value="5" name="me"></Greeting>
    <Greeting value="5" name="me"></Greeting>
    <Greeting value="5" name="me"></Greeting>
</Hello>


I would like the XML output to a file so display like this:

<?xml version="1.0" ?>
<Hello>Opening a new salutation
    <Greeting value=5 name="me"></Greeting>
    <Greeting value=5 name="me"></Greeting>
    <Greeting value=5 name="me"></Greeting>
</Hello>

where the value strings in the Greeting lines are unquoted.

I read the tinyxml.xpp source code tonight and the problem appears to  be in:

doc.SaveFile( "madeByHand2.xml" ) rather than element2->SetAttribute("value","5");


Please show me how to use the TinyXML API and/or Visual Studio 2008 SP1 C++ preprocessor flags to output the xml for integer values as an integer (without quote).


Any help is greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Mark Bullock
Mark Bullock
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of camster123
camster123

ASKER

Hi Mark,
   Thank you for your excellent solution:


Please let me know how to change the following TinyXml C++ code

---------------------------------------------------------------------------------------
 TiXmlDocument doc;
 TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
 doc.LinkEndChild( decl );

 TiXmlElement* element = new TiXmlElement( "Hello" );
 doc.LinkEndChild( element );

 TiXmlText* text = new TiXmlText( "Opening a new salutation" );
 element->LinkEndChild( text );


 for(long i=0; i<3; i++)
 {
 TiXmlElement* element2 = new TiXmlElement( "Greeting" );
 TiXmlText* text2 = new TiXmlText( "" );

 element2->SetAttribute("value","5");
 element2->SetAttribute("name","me");

 element->LinkEndChild( element2 );
 element2->LinkEndChild( text2 );
 }

 doc.SaveFile( "madeByHand2.xml" );
-----------------------------------------------------------------------------------------------------------------

so that when  when value is an element I don't have to use quotes:

Also , could you please let me how to interpret the TinyXML.cpp source code for the EncodeString method if it is relevant?

Thank you,
          Frank
Hi Mark,


Here is the C++ open source code, http://sourceforge.net/p/tinyxml/git/ci/master/tree/tinyxml.cpp for the  TiXmlBase::EncodeString method:

Thank you very much.
   Frank
Mr. Mark Bullock,
   I believe I tried what you suggested yesterday evening when I tried

std::stringstream strStream;
strStream  << "Value " << 5  << " Name " << m_name;    
TiXmlElement* ti = new TiXmlElement( strStream.str() );

I showed that to my manager and he does not like it.

Please let me what else we could try.

Thank you.
     Frank
Hi Mark,
   I just tested your answer from last night as shown here:

http://stackoverflow.com/questions/6017672/tinyxml-how-to-add-an-element.

I am accepting your solution now.

Thank you.
      Frank
Mark's  answer was very helpful when I traced through the tinyxml.cpp code this morning.