Excuse my ignorance but this is my first hour looking at tinyxml. I am trying to create a document from scratch - not even a raw buffer to begin with. I want to create a root node and add elements to it then save the file or send it over the network etc. From looking at it it seems like you have to start with a root node and a first element cause everything seemse to be InsertAfterXXXX. Please help.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks a bunch! Also is there a reverse of Parse for a document - i.e. something that gives me a raw buffer representing a document that I have already created so I can send it over the network?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is the function I'm using :
std::string get_xml_string (const TiXmlNode * XNp_ptr)
{
std::string s;
std::ostringstream os_stream (std::ostringstream::out);
Excuse my ignorance but this is my first hour looking at tinyxml. I am trying to create a document from scratch - not even a raw buffer to begin with. I want to create a root node and add elements to it then save the file or send it over the network etc. From looking at it it seems like you have to start with a root node and a first element cause everything seemse to be InsertAfterXXXX. Please help.
Here is the smallest document you can create from scratch :
TiXmlDocument doc;
TiXmlElement elem ("root_node");
doc . InsertEndChild (elem);
doc . SaveFile ("out.xml");
In TinyXML, the root element is a simple child of the document.
Here's another demo. When run, this is the output:
<Message type="text">Hello World!</Message>
// nb: sourceforge removes indenting :(
#include "stdafx.h"
#include "tinyxml.h"
void test2( )
{
TiXmlNode* node = 0;
TiXmlElement* element = 0;
TiXmlDocument doc;
node = doc.LinkEndChild( new TiXmlElement( "Message" ));
node->ToElement()->SetAttribute( "type", "text" );
node->LinkEndChild( new TiXmlText( "Hello World!" ));
printf( "Completed document:\n" );
doc.Print( stdout );
}
int main(int argc, char* argv[])
{
test2( );
return 0;
}
Thanks a bunch! Also is there a reverse of Parse for a document - i.e. something that gives me a raw buffer representing a document that I have already created so I can send it over the network?
Here is the function I'm using :
std::string get_xml_string (const TiXmlNode * XNp_ptr)
{
std::string s;
std::ostringstream os_stream (std::ostringstream::out);
os_stream << * XNp_ptr;
s = os_stream . str ();
return s;
}
I'm quite sure there should be an easier way ... but I haven't found it yet
Or the slightly easier:
std::string str;
str << document0;
:)
lee
THANK YOU... I needed this basic info. I suggest adding some basic startup info to your docs.
Regards,
Diilbert