I write multiplatform project for iPhone and Android. I need to use xml-files for preferences. I use TinyXML for reading xml-files. It needs full path.
I used the same code from function CCTextureCache::sharedTextureCache()->addImage in my function GetFullPath to get full path of the file.
class XMLParser: public TiXmlDocument
{
public:
std::string GetFullPath( const std::string & path );
XMLParser( const std::string & path );
~XMLParser();
};
XMLParser::XMLParser( const std::string & path ):TiXmlDocument()
{
assert( LoadFile( GetFullPath( path ) ) );
}
std::string XMLParser::GetFullPath( const std::string & path )
{
std::string pathKey = path;
CCFileUtils::ccRemoveHDSuffixFromFile(pathKey);
pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str());
return pathKey;
}
I tried:
…
XMLParser doc( “utf8test.xml” );
…
On iOS devices works great. but on Android it crushed, because cannot find the file.
how to make it work?
On android, the resources are packed into apk, which is a zip file. So you should use CCFileUtils::getFileData() to retrive the data from the apk, and then do with it.
thanks, works great:
string fullPath( XMLFile::GetFullPath( path ) );
CCFileData data( fullPath.c_str(), “rb”);
LoadFileFromCharArr( (char *) data.getBuffer(), data.getSize() );
I have the same problem, now I can get the data with
CCFileData data( fullPath.c_str(), "rb");
but I don’t know how to deal with the charArr data in tinyXML, seems that there is no corresponding interfaces.
I have the same problem, now I can get the data with
CCFileData data( fullPath.c_str(), "rb");
but I don’t know how to deal with the charArr data in tinyXML, seems that there is no corresponding interfaces.
I added the following function to deal with char array:
bool TiXmlDocument::LoadFileWithCharArr( char * arr, int length, TiXmlEncoding encoding )
{
char* buf = new char[ length+1 ];
buf[0] = 0;
memcpy ( buf, arr, length+1 );
// Process the buffer in place to normalize new lines. (See comment above.)
// Copies from the 'p' to 'q' pointer, where p can advance faster if
// a newline-carriage return is hit.
//
// Wikipedia:
// Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or
// CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A)...
// * LF: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
// * CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
// * CR: Commodore 8-bit machines, Apple II family, Mac OS up to version 9 and OS-9
const char* p = buf; // the read head
char* q = buf; // the write head
const char CR = 0x0d;
const char LF = 0x0a;
buf[length] = 0;
while( *p ) {
assert( p < (buf+length) );
assert( q <= (buf+length) );
assert( q <= p );
if ( *p == CR ) {
*q++ = LF;
p++;
if ( *p == LF ) { // check for CR+LF (and skip LF)
p++;
}
}
else {
*q++ = *p++;
}
}
assert( q <= (buf+length) );
*q = 0;
Parse( buf, 0, encoding );
delete [] buf;
return !Error();
}