nux-1.14.0
TiXmlDocument Class Reference

Always the top level node. More...

#include <NuxCore/TinyXML/tinyxml.h>

Inheritance diagram for TiXmlDocument:
TiXmlNode TiXmlBase

List of all members.

Public Member Functions

 TiXmlDocument ()
 Create an empty document, that has no name.
 TiXmlDocument (const char *documentName)
 Create a document with a name. The name of the document is also the filename of the xml.
 TiXmlDocument (const TiXmlDocument &copy)
void operator= (const TiXmlDocument &copy)
bool LoadFile (TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
 Load a file using the current document value.
bool SaveFile () const
 Save a file using the current document value. Returns true if successful.
bool LoadFile (const char *filename, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
 Load a file using the given filename. Returns true if successful.
bool SaveFile (const char *filename) const
 Save a file using the given filename. Returns true if successful.
bool LoadFile (FILE *, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
 Load a file using the given FILE*.
bool SaveFile (FILE *) const
 Save a file using the given FILE*. Returns true if successful.
virtual const char * Parse (const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
 Parse the given null terminated block of xml data.
const TiXmlElementRootElement () const
 Get the root element -- the only top level element -- of the document.
TiXmlElementRootElement ()
bool Error () const
 If an error occurs, Error will be set to true.
const char * ErrorDesc () const
 Contains a textual (english) description of the error if one occurs.
int ErrorId () const
 Generally, you probably want the error string ( ErrorDesc() ).
int ErrorRow () const
 Returns the location (if known) of the error.
int ErrorCol () const
void SetTabSize (int _tabsize)
 SetTabSize() allows the error reporting functions (ErrorRow() and ErrorCol()) to report the correct values for row and column.
int TabSize () const
void ClearError ()
 If you have handled the error, it can be reset with this call.
void Print () const
 Write the document to standard out using formatted printing ("pretty print").
virtual void Print (FILE *cfile, int depth=0) const
 Print this Document to a FILE stream.
void SetError (int err, const char *errorLocation, TiXmlParsingData *prevData, TiXmlEncoding encoding)
virtual const TiXmlDocumentToDocument () const
virtual TiXmlDocumentToDocument ()
virtual bool Accept (TiXmlVisitor *content) const
 Walk the XML tree visiting this node and all of its children.

Protected Member Functions

virtual TiXmlNodeClone () const
 Create an exact duplicate of this node and return it.

Detailed Description

Always the top level node.

A document binds together all the XML pieces. It can be saved, loaded, and printed to the screen. The 'value' of a document node is the xml file name.

Definition at line 1738 of file tinyxml.h.


Member Function Documentation

void TiXmlDocument::ClearError ( ) [inline]

If you have handled the error, it can be reset with this call.

The error state is automatically cleared if you Parse a new XML block.

Definition at line 1887 of file tinyxml.h.

Referenced by Parse(), and TiXmlDocument().

  {
    error = false;
    errorId = 0;
    errorDesc = "";
    errorLocation.row = errorLocation.col = 0;
    //errorLocation.last = 0;
  }
TiXmlNode * TiXmlDocument::Clone ( ) const [protected, virtual]

Create an exact duplicate of this node and return it.

The memory must be deleted by the caller.

Implements TiXmlNode.

Definition at line 1270 of file tinyxml.cpp.

References TiXmlDocument().

{
  TiXmlDocument *clone = new TiXmlDocument();

  if ( !clone )
    return 0;

  CopyTo ( clone );
  return clone;
}
bool TiXmlDocument::Error ( ) const [inline]

If an error occurs, Error will be set to true.

Also,

  • The ErrorId() will contain the integer identifier of the error (not generally useful)
  • The ErrorDesc() method will return the name of the error. (very useful)
  • The ErrorRow() and ErrorCol() will return the location of the error (if known)

Definition at line 1815 of file tinyxml.h.

Referenced by LoadFile().

  {
    return error;
  }
int TiXmlDocument::ErrorCol ( ) const [inline]

< The column where the error occured. See ErrorRow()

Definition at line 1845 of file tinyxml.h.

  {
    return errorLocation.col + 1;  
  }
int TiXmlDocument::ErrorId ( ) const [inline]

Generally, you probably want the error string ( ErrorDesc() ).

But if you prefer the ErrorId, this function will fetch it.

Definition at line 1829 of file tinyxml.h.

  {
    return errorId;
  }
int TiXmlDocument::ErrorRow ( ) const [inline]

Returns the location (if known) of the error.

The first column is column 1, and the first row is row 1. A value of 0 means the row and column wasn't applicable (memory errors, for example, have no row/column) or the parser lost the error. (An error in the error reporting, in that case.)

See also:
SetTabSize, Row, Column

Definition at line 1841 of file tinyxml.h.

  {
    return errorLocation.row + 1;
  }
bool TiXmlDocument::LoadFile ( TiXmlEncoding  encoding = TIXML_DEFAULT_ENCODING)

Load a file using the current document value.

Returns true if successful. Will delete any existing document data before loading.

Definition at line 1037 of file tinyxml.cpp.

References TiXmlNode::Value().

Referenced by LoadFile().

{
  // See STL_STRING_BUG below.
  //StringToBuffer buf( value );

  return LoadFile ( Value(), encoding );
}
bool TiXmlDocument::LoadFile ( FILE *  file,
TiXmlEncoding  encoding = TIXML_DEFAULT_ENCODING 
)

Load a file using the given FILE*.

Returns true if successful. Note that this method doesn't stream - the entire object pointed at by the FILE* will be interpreted as an XML file. TinyXML doesn't stream in XML from the current file location. Streaming may be added in the future.

Definition at line 1086 of file tinyxml.cpp.

References TiXmlNode::Clear(), Error(), and Parse().

{
  if ( !file )
  {
    SetError ( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
    return false;
  }

  // Delete the existing data:
  Clear();
  location.Clear();

  // Get the file size, so we can pre-allocate the string. HUGE speed impact.
  long length = 0;
  fseek ( file, 0, SEEK_END );
  length = ftell ( file );
  fseek ( file, 0, SEEK_SET );

  // Strange case, but good to handle up front.
  if ( length <= 0 )
  {
    SetError ( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
    return false;
  }

  // If we have a file, assume it is all one big XML file, and read it in.
  // The document parser may decide the document ends sooner than the entire file, however.
  TIXML_STRING data;
  data.reserve ( length );

  // Subtle bug here. TinyXml did use fgets. But from the XML spec:
  // 2.11 End-of-Line Handling
  // <snip>
  // <quote>
  // ...the XML processor MUST behave as if it normalized all line breaks in external
  // parsed entities (including the document entity) on input, before parsing, by translating
  // both the two-character sequence #xD #xA and any #xD that is not followed by #xA to
  // a single #xA character.
  // </quote>
  //
  // It is not clear fgets does that, and certainly isn't clear it works cross platform.
  // Generally, you expect fgets to translate from the convention of the OS to the c/unix
  // convention, and not work generally.

  /*
  while( fgets( buf, sizeof(buf), file ) )
  {
        data += buf;
  }
  */

  char *buf = new char[ length+1 ];
  buf[0] = 0;

  if ( fread ( buf, length, 1, file ) != 1 )
  {
    delete [] buf;
    SetError ( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
    return false;
  }

  const char *lastPos = buf;

  const char *p = buf;

  buf[length] = 0;

  while ( *p )
  {
    assert ( p < (buf + length) );

    if ( *p == 0xa )
    {
      // Newline character. No special rules for this. Append all the characters
      // since the last string, and include the newline.
      data.append ( lastPos, (p - lastPos + 1) );       // append, include the newline
      ++p;                                                                      // move past the newline
      lastPos = p;                                                      // and point to the new buffer (may be 0)
      assert ( p <= (buf + length) );
    }
    else if ( *p == 0xd )
    {
      // Carriage return. Append what we have so far, then
      // handle moving forward in the buffer.
      if ( (p - lastPos) > 0 )
      {
        data.append ( lastPos, p - lastPos );   // do not add the CR
      }

      data += (char) 0xa;                                               // a proper newline

      if ( * (p + 1) == 0xa )
      {
        // Carriage return - new line sequence
        p += 2;
        lastPos = p;
        assert ( p <= (buf + length) );
      }
      else
      {
        // it was followed by something else...that is presumably characters again.
        ++p;
        lastPos = p;
        assert ( p <= (buf + length) );
      }
    }
    else
    {
      ++p;
    }
  }

  // Handle any left over characters.
  if ( p - lastPos )
  {
    data.append ( lastPos, p - lastPos );
  }

  delete [] buf;
  buf = 0;

  Parse ( data.c_str(), 0, encoding );

  if (  Error() )
    return false;
  else
    return true;
}
const char * TiXmlDocument::Parse ( const char *  p,
TiXmlParsingData data = 0,
TiXmlEncoding  encoding = TIXML_DEFAULT_ENCODING 
) [virtual]

Parse the given null terminated block of xml data.

Passing in an encoding to this method (either TIXML_ENCODING_LEGACY or TIXML_ENCODING_UTF8 will force TinyXml to use that encoding, regardless of what TinyXml might otherwise try to detect.

Implements TiXmlBase.

Definition at line 775 of file tinyxmlparser.cpp.

References ClearError(), TiXmlDeclaration::Encoding(), TiXmlNode::LinkEndChild(), and TiXmlNode::ToDeclaration().

Referenced by LoadFile().

{
  ClearError();

  // Parse away, at the document level. Since a document
  // contains nothing but other tags, most of what happens
  // here is skipping white space.
  if ( !p || !*p )
  {
    SetError ( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
    return 0;
  }

  // Note that, for a document, this needs to come
  // before the while space skip, so that parsing
  // starts from the pointer we are given.
  location.Clear();

  if ( prevData )
  {
    location.row = prevData->cursor.row;
    location.col = prevData->cursor.col;
  }
  else
  {
    location.row = 0;
    location.col = 0;
  }

  TiXmlParsingData data ( p, TabSize(), location.row, location.col );
  location = data.Cursor();

  if ( encoding == TIXML_ENCODING_UNKNOWN )
  {
    // Check for the Microsoft UTF-8 lead bytes.
    const unsigned char *pU = (const unsigned char *) p;

    if (        * (pU + 0) && * (pU + 0) == TIXML_UTF_LEAD_0
          && * (pU + 1) && * (pU + 1) == TIXML_UTF_LEAD_1
          && * (pU + 2) && * (pU + 2) == TIXML_UTF_LEAD_2 )
    {
      encoding = TIXML_ENCODING_UTF8;
      useMicrosoftBOM = true;
    }
  }

  p = SkipWhiteSpace ( p, encoding );

  if ( !p )
  {
    SetError ( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
    return 0;
  }

  while ( p && *p )
  {
    TiXmlNode *node = Identify ( p, encoding );

    if ( node )
    {
      p = node->Parse ( p, &data, encoding );
      LinkEndChild ( node );
      
      /* LinkEndChild may potentially free the node.
         If this happens we should break to avoid dereferencing it */
      if ( !node )
        break;
    }
    else
    {
      break;
    }

    // Did we get encoding info?
    if (    encoding == TIXML_ENCODING_UNKNOWN
            && node->ToDeclaration() )
    {
      TiXmlDeclaration *dec = node->ToDeclaration();
      const char *enc = dec->Encoding();
      assert ( enc );

      if ( *enc == 0 )
        encoding = TIXML_ENCODING_UTF8;
      else if ( StringEqual ( enc, "UTF-8", true, TIXML_ENCODING_UNKNOWN ) )
        encoding = TIXML_ENCODING_UTF8;
      else if ( StringEqual ( enc, "UTF8", true, TIXML_ENCODING_UNKNOWN ) )
        encoding = TIXML_ENCODING_UTF8; // incorrect, but be nice
      else
        encoding = TIXML_ENCODING_LEGACY;
    }

    p = SkipWhiteSpace ( p, encoding );
  }

  // Was this empty?
  if ( !firstChild )
  {
    SetError ( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, encoding );
    return 0;
  }

  // All is well.
  return p;
}
void TiXmlDocument::Print ( ) const [inline]

Write the document to standard out using formatted printing ("pretty print").

Definition at line 1897 of file tinyxml.h.

Referenced by SaveFile().

  {
    Print ( stdout, 0 );
  }
const TiXmlElement* TiXmlDocument::RootElement ( ) const [inline]

Get the root element -- the only top level element -- of the document.

In well formed XML, there should only be one. TinyXml is tolerant of multiple elements at the document level.

Definition at line 1801 of file tinyxml.h.

References TiXmlNode::FirstChildElement().

  {
    return FirstChildElement();
  }
void TiXmlDocument::SetTabSize ( int  _tabsize) [inline]

SetTabSize() allows the error reporting functions (ErrorRow() and ErrorCol()) to report the correct values for row and column.

It does not change the output or input in any way.

By calling this method, with a tab size greater than 0, the row and column of each node and attribute is stored when the file is loaded. Very useful for tracking the DOM back in to the source file.

The tab size is required for calculating the location of nodes. If not set, the default of 4 is used. The tabsize is set per document. Setting the tabsize to 0 disables row/column tracking.

Note that row and column tracking is not supported when using operator>>.

The tab size needs to be enabled before the parse or load. Correct usage:

  	TiXmlDocument doc;
  	doc.SetTabSize( 8 );
  	doc.Load( "myfile.xml" );
  	
See also:
Row, Column

Definition at line 1874 of file tinyxml.h.

  {
    tabsize = _tabsize;
  }
virtual TiXmlDocument* TiXmlDocument::ToDocument ( ) [inline, virtual]

< Cast to a more defined type. Will return null not of the requested type.

Reimplemented from TiXmlNode.

Definition at line 1917 of file tinyxml.h.

  {
    return this;  
  }
virtual const TiXmlDocument* TiXmlDocument::ToDocument ( ) const [inline, virtual]

< Cast to a more defined type. Will return null not of the requested type.

Reimplemented from TiXmlNode.

Definition at line 1913 of file tinyxml.h.

  {
    return this;  
  }

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends