Hmm. You might need to read some more C++ basics, but I'll try to help.
A TiXmlDocument instance is an object of class TiXmlDocument. If you use new() then it is allocated dynamically on the heap; if you do not use new (the second form in your example) then it is allocated statically on the stack. This is all basic C++ stuff.
The objects you add to a TiXmlDocument are owned by that document - they will be deleted when that document is deleted.
Therefore in this code:
void write_simple_doc( )
{
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
TiXmlElement * element = new TiXmlElement( "Hello" );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
doc.LinkEndChild( decl );
doc.LinkEndChild( element );
}
at the end of this block, 'doc' will go out of scope, and it will delete all the TiXmlElement/Text nodes that were allocated automatically.
You can look into TiXmlHandles too, but I think thats mostly for retrieving data from a pre-built DOM.
HTH
Ellers
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I am sorry to ask this question but since I am a newbie regarding the xml thing.
I would like to create a XML document with the following format :
<?xml version="1.0" encoding="ISO-8859-1"?>
<resource version="0.1">
<object class="label">
<pos>100,50</pos>
<size>320,200</size>
<color>blue</color>
</object>
<object class="editbox">
<pos>100,50</pos>
<size>320,200</size>
<color>blue</color>
</object>
</resource>
the node are obtained by parsing a list :
bool CDummy::CreateXML()
{
LPCTSTR szProlog = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
"<resource version=\"0.1\">"
"</resource>";
TiXmlDocument Doc;
Doc.Parse( szProlog );
while (posDynControl){
insert new node object with attribute class="label"
go inside this node and add the child element pos, size and color
get out the current node
}
}
How can I do this ?
I suppose that first I need to go to resource node but after I don't know
Aspects of what you want are addressed in this tutorial:
http://software.ellerton.net/tinyxml.html
e.g. at:
http://software.ellerton.net/tinyxml.html#building-documents-pragmatically
Try that for a start, and post back here with more specifics if you need to...
Ellers
thnaks
Actually now I have a question, in the tutorial they are using
pointers so I suppose I need to delete them after use :
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild( decl );
TiXmlElement * element = new TiXmlElement( "Hello" );
doc.LinkEndChild( element );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
What if I want to use object, I tried and it compiled fine but xml was not good.
TiXmlDeclaration decl( "1.0", "", "" );
doc.InsertEndChild( decl );
...
Is there any example with objects, ?
Hmm. You might need to read some more C++ basics, but I'll try to help.
A TiXmlDocument instance is an object of class TiXmlDocument. If you use new() then it is allocated dynamically on the heap; if you do not use new (the second form in your example) then it is allocated statically on the stack. This is all basic C++ stuff.
The objects you add to a TiXmlDocument are owned by that document - they will be deleted when that document is deleted.
Therefore in this code:
void write_simple_doc( )
{
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
TiXmlElement * element = new TiXmlElement( "Hello" );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
doc.LinkEndChild( decl );
doc.LinkEndChild( element );
}
at the end of this block, 'doc' will go out of scope, and it will delete all the TiXmlElement/Text nodes that were allocated automatically.
You can look into TiXmlHandles too, but I think thats mostly for retrieving data from a pre-built DOM.
HTH
Ellers