tinyxml2
A simple, small, efficient, C++ XML parser that can be easily integrating into other programs.

Function LoadFile

Synopsis

#include <tinyxml2/tinyxml2.h>

XMLError tinyxml2::XMLDocument::LoadFile(const char *filename)

XMLError tinyxml2::XMLDocument::LoadFile(FILE *)

Description

Load an XML file from disk.

XMLError tinyxml2::XMLDocument::LoadFile(const char *filename)

Load an XML file from disk. Returns XML_NO_ERROR (0) on success, or an errorID.

XMLError tinyxml2::XMLDocument::LoadFile(FILE *)

Load an XML file from disk. You are responsible for providing and closing the FILE*.

NOTE: The file should be opened as binary ("rb") not text in order for TinyXML-2 to correctly do newline normalization.

Returns XML_NO_ERROR (0) on success, or an errorID.

Examples

Load an XML File from disk:

XMLDocument doc;
doc.LoadFile( "resources/dream.xml" );
return doc.ErrorID();

Source

From lines 1814-1825 in tinyxml2.cpp

XMLError XMLDocument::LoadFile( const char* filename )
{
    Clear();
    FILE* fp = callfopen( filename, "rb" );
    if ( !fp ) {
        SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
        return _errorID;
    }
    LoadFile( fp );
    fclose( fp );
    return _errorID;
}

From lines 1828-1863 in tinyxml2.cpp

XMLError XMLDocument::LoadFile( FILE* fp )
{
    Clear();

    fseek( fp, 0, SEEK_SET );
    if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
        SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
        return _errorID;
    }

    fseek( fp, 0, SEEK_END );
    const long filelength = ftell( fp );
    fseek( fp, 0, SEEK_SET );
    if ( filelength == -1L ) {
        SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
        return _errorID;
    }

    const size_t size = filelength;
    if ( size == 0 ) {
        SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
        return _errorID;
    }

    _charBuffer = new char[size+1];
    size_t read = fread( _charBuffer, 1, size, fp );
    if ( read != size ) {
        SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
        return _errorID;
    }

    _charBuffer[size] = 0;

    Parse();
    return _errorID;
}


Discussions

There are 0 discussions relevant for this page, and 0 discussions in the whole project.




Add Discussion

Log in to comment