Menu

Baffled on how to "read" node & element data

Lynn Allan
2002-01-30
2002-01-30
  • Lynn Allan

    Lynn Allan - 2002-01-30

    I'm trying to get up to speed with TinyXml.  It looks close to what I've been hunting for; a tiny footprint utility to allow an xml textfile to provide persistent data.

    I got the xmltest.cpp to work ok, but I'm baffled how to "read" values and attributes.  I'm unfamiliar with std::string, and can't figure out how to confirm the contents of the nodes and elements.

    Below is a snippet from xmltest.cpp.  The additional lines with //?? don't work with the Visual C++ 6.0 compiler, or crash during execution.  What am I doing wrong?  In the debugger, I can see node->value and element->value, but I'm unclear on the syntax and/or casts to be able to do a printf or cout.

    Do you have additional test programs?  I would especially be interested in a program that dumped the contents of an xml file that has been read into memory.

    Regards,
    Lynn Allan

    // Get the "ToDo" element.
    // It is a child of the document, and can be selected by name.
    node = doc.FirstChild( "ToDo" );
    assert( node );
    todoElement = node->ToElement();
    assert( todoElement  );
    printf("Type: %d\n", todoElement->Type());  //This works, but next line doesn't
    //??printf("Value: %s\n", todoElement->Value());

    // Going to the toy store is now our second priority...
    // So set the "priority" attribute of the first item in the list.
    node = todoElement->FirstChild();
    assert( node );
    itemElement = node->ToElement();
    assert( itemElement  );
    itemElement->SetAttribute( "priority", 2 );
    //??int num;
    //??printf("Attribute: %s", itemElement->Attribute("priority", &num);

     
    • Lee Thomason

      Lee Thomason - 2002-01-30

      I apologize if I sound didactic, but you should really familiarize yourself with the STL string:

      http://www.msoe.edu/eecs/cese/resources/stl/string.htm

      seems like a reasonably good site. If you mix C and C++ strings, you need to use the c_str() method, to cast one from the other.

      string s = "hello";
      printf( "%s", s.c_str() );

      Hope that helps!
      lee

       
    • Yves Berquin

      Yves Berquin - 2002-01-30

      Hi Lynn,

      If don't want to (or don't know how to) use std::string, You can do the following :
      Either you use the c_str () member that converts a std::string to a usual "const char *" pointer for your C / C++ program, or you add up member functions inside the TinyXML classes that return const char * like this :
      const char * CValue () {return Value . c_str ();}

      For Attributes, you can do the same :
      const char * CAttribute (const char * att, int i)
      {
      return Attribute (att, i) . c_str ();
      }

      Yves

       
    • Lynn Allan

      Lynn Allan - 2002-01-30

      Thanks Lee and Yves.  Your guidance was very helpful for this STL and TinyXml newbie.  Oddly, I had tried to convert the printf statements to iostream cout, and had similar problems.

      Well, back to figuring out tinyxml!

      Regards,
      Lynn Allan

       

Log in to post a comment.