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);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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);
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
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
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