Menu

Tutorial?

Anonymous
2005-04-06
2013-05-20
  • Anonymous

    Anonymous - 2005-04-06

    Is there a TinyXML tutorial? I'm a C++ novice and I'm trying to read a XML file into a linked list. Unfortunately I'm not having much success.

    So, is there a resource for me to learn how to use TinyXML?

    Thanks in advance

     
    • Ellers

      Ellers - 2005-04-06

      Good question. There's been some recent discussion about this. I've started one, but be warned that this is preliminary and is not yet the proper/final location for it:

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

      Also, note that this is a TinyXml tutorial - it doesn't in any way teach you C++. If you've got C++ problems (i.e. not specific to TinyXml) its probably best to post to a C++ specific forum :)

      The most authoritative source of TinyXml programming is currently the test program, included in the distribution.

      See also:
      https://sourceforge.net/forum/forum.php?thread_id=1259479&forum_id=172103

      HTH
      Ellers

       
      • Anonymous

        Anonymous - 2005-04-11

        Thanks for the help, Ellers. It certainly shed some light onto my understanding of the workings of TinyXML. Unfortunately your tutorial and the sourceforge thread you suggested didn't show how to extract information from a XML file. Do you know where I can find something that shows me how to do it properly?

        Thanks for the help

         
        • Ellers

          Ellers - 2005-04-12

          Hey GB

          Looking back at your original post, I see your point.

          So, you're after an example of a function that reads an XML file, and outputs a linked list?

          Can you give me a more specific example that you'd like, and I'll see what I can do?

          Ellers

           
          • Anonymous

            Anonymous - 2005-04-12

            After reading the TinyXML docs and xmltest.cpp, I believe I've finally succeeded. Here is a code snippet. Criticism is more than welcome.

            //xml file:
            <?xml version="1.0"?>
            <data>
                <datum>
                    <value1>1</value1>
                    <value2>2</value2>
                </datum>
                <datum>
                    <value1>3</value1>
                    <value2>4</value2>
                </datum>
            </data>

            // source code:
            struct sData
            {
                int n1, n2;
            };

            bool load_XML_file(vector<sData> &list)
            {
                TiXmlDocument doc("data.xml");
                if (!doc.LoadFile())
                {
                    cout << "Error loading XML file. Aborting..." << endl;
                    return false;
                }
                cout << "Succeeded loading XML file!" << endl;
               
                // passing the XML doc into the list
                sData data;
                TiXmlHandle handle(&doc);
                TiXmlElement *element = handle.FirstChild("data").FirstChild("datum").Element();
                for(element;element;element=element->NextSiblingElement())
                {
                    data.n1 = atoi(element->FirstChild("value1")->FirstChild()->ToText()->Value());
                    data.n2 = atoi(element->FirstChild("value2")->FirstChild()->ToText()->Value());
                    list.push_back(data);
                }
               
                return true;
            }

             
            • Ellers

              Ellers - 2005-04-12

              That looks good :)

              I did my own version and will add it to the tutorial at http://software.ellerton.net/tinyxml.html soon.

              Given this xml:

              <?xml version="1.0" ?>
              <names>
              <name>Foo</name>
              <name>Bar</name>
              </names>

              and these functions:

              #include <list>

              typedef std::list< std::string > string_list;

              void print_list( const string_list & data )
              {
                  string_list::const_iterator iter;
                  int i;

                  printf( "List (%d items):\n", data.size());
                  for ( i = 1, iter = data.begin(); iter != data.end(); iter++, i++ )
                  {
                      printf( "%d. Item: %s\n", i, (*iter).c_str());
                  }
              }

              void demo_convert_to_list1( )
              {
                  // load xml
                  TiXmlDocument doc( "names.xml" );
                  bool loadOkay = doc.LoadFile();

                  if ( !loadOkay )
                  {
                      printf( "Load failed\n" );
                      return;
                  }

                  // iterate over contents, add to list

                  // This approach is "ok" - but not very robust.
                  // For example, if you remove the text from <name>text</name> it will not
                  // handle it gracefully.
                  string_list list;
                  TiXmlElement * pRoot = doc.RootElement();
                  TiXmlNode * pChild;

                  if ( !pRoot )
                  {
                      printf( "No root node\n" );
                      return;
                  }

                  for ( pChild = pRoot->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
                  {
                      // this should be a <name> element, but could be text, comment, etc
                      if ( pChild->Type() == TiXmlNode::ELEMENT && 0==strcmp( pChild->Value(), "name") )
                      {
                          const char * pText = pChild->FirstChild()->Value();
                          list.push_back( std::string( pText ));
                      }
                     
                  }
                  // show the user what was built
                  print_list( list );
              }

              You'll see this output:

              List (2 items):
              1. Item: Foo
              2. Item: Bar

              As mentioned in the code, this is not super robust - it will work if the XML is good, but will fail in not-so-nice ways if not.

              I plan to add examples with
              - list of (say) SongInfo class objects
              - using Handles rather than pointers

              Comments welcome
              Ellers

               

Log in to post a comment.