Create xml using tinyxml

13.9k Views Asked by At

I have a problem. I need to create a following xml:

<?xml version="1.0" encoding="utf-8"?>
<MyApp value="5" name="me">
</MyApp>

Using c++ code I did the following:

#include <iostream>
#include <string>
#include "tinyxml.h" 
void main(){
    TiXmlDocument doc;
    TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "utf-8", "");
    doc.LinkEndChild( decl );

    TiXmlElement * root;
    root = new TiXmlElement( "MyApp" );  
    root->SetAttribute("value","5");
    root->SetAttribute("name","me");
    doc.LinkEndChild( root );  

    doc.SaveFile( "madeByHand.xml" );
    return 0;
}

I compile it using g++ tinyxml.cpp tinyxmlerror.cpp and tinyxmlparser.cpp. When i want to see the xml i've created. i have an error: it says it's not corectly written. where am i wrong?:)

4

There are 4 best solutions below

0
On

Just in case anyone will need such a structure of an XML-file:

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

_

So here is the code for this:

_

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

TiXmlElement* element2 = new TiXmlElement( "Greeting" );
element2->SetAttribute("value","5");
element2->SetAttribute("name","me");
element->LinkEndChild( element2 );

TiXmlText* text2 = new TiXmlText( "" );
element2->LinkEndChild( text2 );

doc.SaveFile( "madeByHand2.xml" );

_

And an option how to add more then one element using loops (for / while):

_

<?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>

_

And here is the code for this:

_

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" );
0
On

Probably, you have missed " #include "tinystr.h" "?

0
On

you must add tinyxmlparser.cpp tinyxmlerror.cpp tinyxml.cpp tinyxml.h tinystr.cpp tinystr.h to your project

0
On

You are trying to use C++ as some type of a script. You have to create a main() function In which the productive code is placed.

#include <iostream>
#include <string>
#include "tinyxml.h"

void main()
{
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "utf-8", "");
doc.LinkEndChild( decl );
TiXmlElement * root;
    root = new TiXmlElement( "MyApp" );  
        root->SetAttribute("value","5");
        root->SetAttribute("name","me");
        doc.LinkEndChild( root );  
doc.SaveFile( "madeByHand.xml" );
}

After the compilation, you have to start the application from the command line, of course.