Menu

Creating XML file from scratch

Developer
Steven
2005-04-01
2013-05-20
  • Steven

    Steven - 2005-04-01

    Does anybody have an example of this? I am attempting to do this and I ran into some trouble. However, I just need an example of writing and XML file from scratch using TinyXml

     
    • Ellers

      Ellers - 2005-04-02

      The following code produces this xml:

      <?xml version="1.0" ?>
      <Hello>World</Hello>

      // (indenting lost by sourceforge)
      void write_simple_doc( )
      {
          TiXmlDocument doc;
          TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", 0, 0 );
          TiXmlElement * element = new TiXmlElement( "Hello" );
          TiXmlText * text = new TiXmlText( "World" );
          element->LinkEndChild( text );
          doc.LinkEndChild( decl );
          doc.LinkEndChild( element );
         
          doc.SaveFile( "madeByHand.xml" );
      }

      As this is about using the library, not developing it, the question is better suited to the OpenDiscussion forum.

      Lee and others: I'd like to re-iterate the idea of a "Intro Examples" section on the home page somewhere. It could include: creating a document in memory, simple parsing from file, parsing from string, walking a tree and dumping to stdout, moving children around.

      FWIW
      Ellers

       
    • Lee Thomason

      Lee Thomason - 2005-04-02

      That (once again) is a very good idea; I'll add that within the next week. On the web page it can be added between updates as well so it doesn't need to wait for the next version.

      I'll set up a HOWTO section with the simple (and highly requested ones) first, including reading a text node. You probably blocked that one :).

      Better to have code snippets (to the point) or small working examples (compile & run, but have more "noise" around the key issue)?

      lee

       
    • Steven

      Steven - 2005-04-03

      Thanks Ellers for your help. It is greatly appreciated. (My apologies for the choice of forum)You have no idea how much this helped. I had reviewed the xmltest.cpp, but it doesn't have a clean example of just creating a file from scratch.

      I had gotten as far as the following (using your example):

      void write_simple_doc( )
      {
      TiXmlDocument doc;
      TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", 0, 0 );
      TiXmlElement * element = new TiXmlElement( "Hello" );
      TiXmlText * text = new TiXmlText( "World" );
      element->LinkEndChild( text );
      }

      However, I couldn't figure out how to write it all into the document. It was the "doc.linkendchild()" statements I was missing.

      So, if you have three levels of nested tags. Is it better to create all the elements and text first and then afterwards go and use the doc.LinkEndChild() calls (in the order of the nested pieces) to write it all to the file?

      Is there any other way (operations) to wrtie the elements back into the file other than LinkEndChild?

      Thanks again for your help.

       
    • Ellers

      Ellers - 2005-04-03

      Hi Steven
      You can create the nodes and add them immediately to the parent if you like. The following code creates the identical DOM as the earlier example, but adds the nodes to the parent immediately.

      void write_simple_doc2( )
      {
          // same as first write_simple_doc but add each
          // node as early as possible into the tree.

          TiXmlDocument doc;
          TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", 0, 0 );
          doc.LinkEndChild( decl );
         
          TiXmlElement * element = new TiXmlElement( "Hello" );
          doc.LinkEndChild( element );
         
          TiXmlText * text = new TiXmlText( "World" );
          element->LinkEndChild( text );
         
          doc.SaveFile( "madeByHand2.xml" );
      }

      Note - hopefully this isn't to pedantic - that calling LinkEndChild() isn't "writing elemnts back into the file". There is no file activity going on in the code except in the line doc.SaveFile(). The code is building up a tree-like structure of elements; LinkEndChild() is adding the child node into the graph that the parent is in - thats all. Calling SaveFile() then traverses (visitor pattern-ish) the tree and writes each node to the file in the appropriate way.  Not sure if that answers your question though? Would you prefer a different method or approach than LinkEndChild?

      HTH
      Ellers

       
      • Steven

        Steven - 2005-04-04

        No, LinkEndChild works just fine. I just wanted to make sure I understood how to add.

        I don't have a compiler at home, but if I wanted to create the following xml file.

        <?xml version="1.0" ?>
        <Hello> Opening a new salutation
            <Greeting> How are you? </Greeting>
                 <Language> English </Language>
            <Exclamation> You have children! </Exclamation>
        </Hello>

        Would it look something like this?

        void write_simple_doc3( )
        {
        // same as second write_simple_doc except with more nested tags 
        // node as early as possible into the tree.

        TiXmlDocument doc;
        TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", 0, 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" );
        element.LinkEndChild( element2 );

        TiXmlText * text2 = new TiXmlText( "How are you?" );
        element2->LinkEndChild( text2 );

        TiXmlElement * element3 = new TiXmlElement( "Language" );
        element2.LinkEndChild( element3 );

        TiXmlText * text3 = new TiXmlText( "English" );
        element3->LinkEndChild( text3 );

        TiXmlElement * element4 = new TiXmlElement( "Exclamation" );
        element.LinkEndChild( element4 );

        TiXmlText * text4 = new TiXmlText( "You have children!" );
        element4->LinkEndChild( text4 );

        doc.SaveFile( "madeByHand3.xml" );
        }

        Also, because the parameter for LinkEndChild is of TiXmlNode type, you can add anything of TiXmlText, TiXmlComment, TiXmlElement, etc because they all inherit from TiXmlNode, correct?

        Thanks again for your help.

        Steven

         
        • Ellers

          Ellers - 2005-04-05

          Your code worked almost exactly :) You had "." in two places where "->" should have been. But aside from that the results are just what you expected.

          I've started writing a little example HOWTO. Maybe Lee will find it useful? In any case, here it is:

          http://software.ellerton.net/tinyxml.html
          http://software.ellerton.net/tinyxml.txt

          I use restructuredText as a format, so you can pick between txt/html as your format (and its easier to edit that html ;)

          Let me know what you think...
          Ellers

           
          • Steven

            Steven - 2005-04-07

            Wow. I am impressed on two accounts. First, that I actually got something so close without a handy compiler. Secondly, with your work on the HOWTO. Had I seen that in the beginning. I never would have had to bother you folks. :-) I highly recommend keeping it published somewhere and getting it registered with google. It would be very useful to others.

            I am nearing completion of my project. My tool will be creating about 75k-100k lines of XML for a new database system. Thanks again for your help and to Lee for the small efficient elegance of TinyXML.

             

Log in to post a comment.

MongoDB Logo MongoDB