Search code examples
c++variablesstringconstantstinyxml

Tinyxml to print attributes


I'm trying to get std::string from attribute's value with TinyXml. The only thing I can get is a const char * val, and I can't find any way to convert from const char * to a std::string.

so two possible answers to that: 1. How to get a string of an attribute with TinyXml? 2. How to convert const char * val to string val.

this is the code I have now:

TiXmlElement* data;
data->Attribute("some_name"); // return const char * which seems like unconvertible.

After googeling, I tried this:

char * not_const= const_cast<char *> (data->Attribute("some_name"));

There are no errors in the code itself, but after compiling and running I get exceptions.


Solution

  • std::string has a constructor that takes char const*. You don't need a char* for that.

    std::string str = data->Attribute("some_name");
    

    However, be aware that std::string doesn't like NULL values, so don't give it any.