Menu

Help - Creating XML document

tariq
2004-09-09
2013-05-20
  • tariq

    tariq - 2004-09-09

    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.

     
    • Yves Berquin

      Yves Berquin - 2004-09-10

      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.

       
    • Ellers

      Ellers - 2004-09-10

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

       
    • tariq

      tariq - 2004-09-13

      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?

       
      • Yves Berquin

        Yves Berquin - 2004-09-14

        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

         
    • Lee Thomason

      Lee Thomason - 2004-09-15

      Or the slightly easier:

      std::string str;
      str << document0;

      :)

      lee

       
    • diilbert

      diilbert - 2005-02-10

      THANK YOU... I needed this basic info.  I suggest adding some basic startup info to your docs.

      Regards,

      Diilbert

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.