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.
camster123Senior C++ Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA Engineer IIICommented:
When value is an attribute you have to use quotes: value="123"

When value is an element you don't have to use quotes:
<Hello>Opening a new salutation
<Greeting>
<value>5</value>
<name>="me"</name>
</Greeting>
</Hello>

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
camster123Senior C++ Software EngineerAuthor Commented:
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
0
camster123Senior C++ Software EngineerAuthor Commented:
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
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

camster123Senior C++ Software EngineerAuthor Commented:
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
0
camster123Senior C++ Software EngineerAuthor Commented:
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
0
camster123Senior C++ Software EngineerAuthor Commented:
Mark's  answer was very helpful when I traced through the tinyxml.cpp code this morning.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.