Re: [xml] simple questions

Andre Street wrote:

...and now I'm stuck, because I havn't a clue how to cast the retrieved xmlchar* into something usable as a basic data type on my system ie. a char*, or even a c++ 'string' (the data originates as a simple string, no control characters, e.g. '48' or 'pizza').


My current, desperate solution is

//for a c++ string
ostringstream temp;
temp << xmlNodeGetContent(node);
SomeVariable = temp.str();

Which works, but I suspect does something horrible with the wide characters
of UTF-8. Maybe even hazardous.

ASCII is a subset of UTF-8, so whenever you are sure the data really only
contain ASCII (such as '48' or 'pizza') you can treat a 'xmlChar *' as if it
were a 'char *'.

xmlChar *content = xmlNodeGetContent(node);
std::string SomeVariable(static_cast<char*>(content));

will do the trick (UTF-8 is NULL-terminated, too).

Anyway, if anyone could paste a code example of how to get xmlchar* back to
the native char* of the local system, many thanks (before I bail out and try learning
about c++ i/o and interfaces with file handling...)

When you can't be sure about the content being ASCII I would indeed
suggest to use an external library to provide unicode datatypes as well
as conversion routines.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]