Shapeworks Studio  2.1
Shape analysis software suite
List of all members | Public Member Functions | Protected Member Functions
TiXmlDocument Class Reference

#include <tinyxml.h>

+ Inheritance diagram for TiXmlDocument:
+ Collaboration diagram for TiXmlDocument:

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)
 
TiXmlDocumentoperator= (const TiXmlDocument &copy)
 
bool LoadFile (TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
 
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)
 
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)
 
const TiXmlElementRootElement () const
 
TiXmlElementRootElement ()
 
bool Error () const
 
const char * ErrorDesc () const
 Contains a textual (english) description of the error if one occurs.
 
int ErrorId () const
 
int ErrorRow () const
 
int ErrorCol () const
 The column where the error occured. See ErrorRow()
 
void SetTabSize (int _tabsize)
 
int TabSize () const
 
void ClearError ()
 
void Print () const
 
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
 Cast to a more defined type. Will return null not of the requested type.
 
virtual TiXmlDocumentToDocument ()
 Cast to a more defined type. Will return null not of the requested type.
 
virtual bool Accept (TiXmlVisitor *content) const
 
- Public Member Functions inherited from TiXmlNode
const char * Value () const
 
const TIXML_STRING & ValueTStr () const
 
void SetValue (const char *_value)
 
void Clear ()
 Delete all the children of this node. Does not affect 'this'.
 
TiXmlNodeParent ()
 One step up the DOM.
 
const TiXmlNodeParent () const
 
const TiXmlNodeFirstChild () const
 The first child of this node. Will be null if there are no children.
 
TiXmlNodeFirstChild ()
 
const TiXmlNodeFirstChild (const char *value) const
 
TiXmlNodeFirstChild (const char *_value)
 The first child of this node with the matching 'value'. Will be null if none found.
 
const TiXmlNodeLastChild () const
 
TiXmlNodeLastChild ()
 The last child of this node. Will be null if there are no children.
 
const TiXmlNodeLastChild (const char *value) const
 
TiXmlNodeLastChild (const char *_value)
 The last child of this node matching 'value'. Will be null if there are no children.
 
const TiXmlNodeIterateChildren (const TiXmlNode *previous) const
 
TiXmlNodeIterateChildren (const TiXmlNode *previous)
 
const TiXmlNodeIterateChildren (const char *value, const TiXmlNode *previous) const
 This flavor of IterateChildren searches for children with a particular 'value'.
 
TiXmlNodeIterateChildren (const char *_value, const TiXmlNode *previous)
 
TiXmlNodeInsertEndChild (const TiXmlNode &addThis)
 
TiXmlNodeLinkEndChild (TiXmlNode *addThis)
 
TiXmlNodeInsertBeforeChild (TiXmlNode *beforeThis, const TiXmlNode &addThis)
 
TiXmlNodeInsertAfterChild (TiXmlNode *afterThis, const TiXmlNode &addThis)
 
TiXmlNodeReplaceChild (TiXmlNode *replaceThis, const TiXmlNode &withThis)
 
bool RemoveChild (TiXmlNode *removeThis)
 Delete a child of this node.
 
const TiXmlNodePreviousSibling () const
 Navigate to a sibling node.
 
TiXmlNodePreviousSibling ()
 
const TiXmlNodePreviousSibling (const char *) const
 Navigate to a sibling node.
 
TiXmlNodePreviousSibling (const char *_prev)
 
const TiXmlNodeNextSibling () const
 Navigate to a sibling node.
 
TiXmlNodeNextSibling ()
 
const TiXmlNodeNextSibling (const char *) const
 Navigate to a sibling node with the given 'value'.
 
TiXmlNodeNextSibling (const char *_next)
 
const TiXmlElementNextSiblingElement () const
 
TiXmlElementNextSiblingElement ()
 
const TiXmlElementNextSiblingElement (const char *) const
 
TiXmlElementNextSiblingElement (const char *_next)
 
const TiXmlElementFirstChildElement () const
 Convenience function to get through elements.
 
TiXmlElementFirstChildElement ()
 
const TiXmlElementFirstChildElement (const char *_value) const
 Convenience function to get through elements.
 
TiXmlElementFirstChildElement (const char *_value)
 
int Type () const
 
const TiXmlDocumentGetDocument () const
 
TiXmlDocumentGetDocument ()
 
bool NoChildren () const
 Returns true if this node has no children.
 
virtual const TiXmlElementToElement () const
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual const TiXmlCommentToComment () const
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual const TiXmlUnknownToUnknown () const
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual const TiXmlTextToText () const
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual const TiXmlDeclarationToDeclaration () const
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual TiXmlElementToElement ()
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual TiXmlCommentToComment ()
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual TiXmlUnknownToUnknown ()
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual TiXmlTextToText ()
 Cast to a more defined type. Will return null if not of the requested type.
 
virtual TiXmlDeclarationToDeclaration ()
 Cast to a more defined type. Will return null if not of the requested type.
 
- Public Member Functions inherited from TiXmlBase
int Row () const
 
int Column () const
 See Row()
 
void SetUserData (void *user)
 Set a pointer to arbitrary user data.
 
void * GetUserData ()
 Get a pointer to arbitrary user data.
 
const void * GetUserData () const
 Get a pointer to arbitrary user data.
 

Protected Member Functions

virtual TiXmlNodeClone () const
 
- Protected Member Functions inherited from TiXmlNode
 TiXmlNode (NodeType _type)
 
void CopyTo (TiXmlNode *target) const
 
TiXmlNodeIdentify (const char *start, TiXmlEncoding encoding)
 

Additional Inherited Members

- Public Types inherited from TiXmlNode
enum  NodeType {
  TINYXML_DOCUMENT, TINYXML_ELEMENT, TINYXML_COMMENT, TINYXML_UNKNOWN,
  TINYXML_TEXT, TINYXML_DECLARATION, TINYXML_TYPECOUNT
}
 
- Public Types inherited from TiXmlBase
enum  {
  TIXML_NO_ERROR = 0, TIXML_ERROR, TIXML_ERROR_OPENING_FILE, TIXML_ERROR_PARSING_ELEMENT,
  TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME, TIXML_ERROR_READING_ELEMENT_VALUE, TIXML_ERROR_READING_ATTRIBUTES, TIXML_ERROR_PARSING_EMPTY,
  TIXML_ERROR_READING_END_TAG, TIXML_ERROR_PARSING_UNKNOWN, TIXML_ERROR_PARSING_COMMENT, TIXML_ERROR_PARSING_DECLARATION,
  TIXML_ERROR_DOCUMENT_EMPTY, TIXML_ERROR_EMBEDDED_NULL, TIXML_ERROR_PARSING_CDATA, TIXML_ERROR_DOCUMENT_TOP_ONLY,
  TIXML_ERROR_STRING_COUNT
}
 
- Static Public Member Functions inherited from TiXmlBase
static void SetCondenseWhiteSpace (bool condense)
 
static bool IsWhiteSpaceCondensed ()
 Return the current white space setting.
 
static void EncodeString (const TIXML_STRING &str, TIXML_STRING *out)
 
- Static Public Attributes inherited from TiXmlBase
static const int utf8ByteTable [256]
 
- Static Protected Member Functions inherited from TiXmlBase
static const char * SkipWhiteSpace (const char *, TiXmlEncoding encoding)
 
static bool IsWhiteSpace (char c)
 
static bool IsWhiteSpace (int c)
 
static const char * ReadName (const char *p, TIXML_STRING *name, TiXmlEncoding encoding)
 
static const char * ReadText (const char *in, TIXML_STRING *text, bool ignoreWhiteSpace, const char *endTag, bool ignoreCase, TiXmlEncoding encoding)
 
static const char * GetEntity (const char *in, char *value, int *length, TiXmlEncoding encoding)
 
static const char * GetChar (const char *p, char *_value, int *length, TiXmlEncoding encoding)
 
static bool StringEqual (const char *p, const char *endTag, bool ignoreCase, TiXmlEncoding encoding)
 
static int IsAlpha (unsigned char anyByte, TiXmlEncoding encoding)
 
static int IsAlphaNum (unsigned char anyByte, TiXmlEncoding encoding)
 
static int ToLower (int v, TiXmlEncoding encoding)
 
static void ConvertUTF32ToUTF8 (unsigned long input, char *output, int *length)
 
- Protected Attributes inherited from TiXmlNode
TiXmlNodeparent
 
NodeType type
 
TiXmlNodefirstChild
 
TiXmlNodelastChild
 
TIXML_STRING value
 
TiXmlNodeprev
 
TiXmlNodenext
 
- Protected Attributes inherited from TiXmlBase
TiXmlCursor location
 
void * userData
 Field containing a generic user pointer.
 
- Static Protected Attributes inherited from TiXmlBase
static const char * errorString [TIXML_ERROR_STRING_COUNT]
 

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 1393 of file tinyxml.h.

Member Function Documentation

bool TiXmlDocument::Accept ( TiXmlVisitor content) const
virtual

Walk the XML tree visiting this node and all of its children.

Implements TiXmlNode.

Definition at line 1156 of file tinyxml.cpp.

1157 {
1158  if ( visitor->VisitEnter( *this ) )
1159  {
1160  for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
1161  {
1162  if ( !node->Accept( visitor ) )
1163  break;
1164  }
1165  }
1166  return visitor->VisitExit( *this );
1167 }
const TiXmlNode * NextSibling() const
Navigate to a sibling node.
Definition: tinyxml.h:631
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:522
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 1511 of file tinyxml.h.

1511  { error = false;
1512  errorId = 0;
1513  errorDesc = "";
1514  errorLocation.row = errorLocation.col = 0;
1515  //errorLocation.last = 0;
1516  }
TiXmlNode * TiXmlDocument::Clone ( ) const
protectedvirtual

Create an exact duplicate of this node and return it. The memory must be deleted by the caller.

Implements TiXmlNode.

Definition at line 1134 of file tinyxml.cpp.

1135 {
1136  TiXmlDocument* clone = new TiXmlDocument();
1137  if ( !clone )
1138  return 0;
1139 
1140  CopyTo( clone );
1141  return clone;
1142 }
TiXmlDocument()
Create an empty document, that has no name.
Definition: tinyxml.cpp:913
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 1460 of file tinyxml.h.

1460 { return error; }
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 1468 of file tinyxml.h.

1468 { 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 1477 of file tinyxml.h.

1477 { 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 954 of file tinyxml.cpp.

955 {
956  return LoadFile( Value(), encoding );
957 }
const char * Value() const
Definition: tinyxml.h:487
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Definition: tinyxml.cpp:954
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 986 of file tinyxml.cpp.

987 {
988  if ( !file )
989  {
990  SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
991  return false;
992  }
993 
994  // Delete the existing data:
995  Clear();
996  location.Clear();
997 
998  // Get the file size, so we can pre-allocate the string. HUGE speed impact.
999  long length = 0;
1000  fseek( file, 0, SEEK_END );
1001  length = ftell( file );
1002  fseek( file, 0, SEEK_SET );
1003 
1004  // Strange case, but good to handle up front.
1005  if ( length <= 0 )
1006  {
1007  SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
1008  return false;
1009  }
1010 
1011  // Subtle bug here. TinyXml did use fgets. But from the XML spec:
1012  // 2.11 End-of-Line Handling
1013  // <snip>
1014  // <quote>
1015  // ...the XML processor MUST behave as if it normalized all line breaks in external
1016  // parsed entities (including the document entity) on input, before parsing, by translating
1017  // both the two-character sequence #xD #xA and any #xD that is not followed by #xA to
1018  // a single #xA character.
1019  // </quote>
1020  //
1021  // It is not clear fgets does that, and certainly isn't clear it works cross platform.
1022  // Generally, you expect fgets to translate from the convention of the OS to the c/unix
1023  // convention, and not work generally.
1024 
1025  /*
1026  while( fgets( buf, sizeof(buf), file ) )
1027  {
1028  data += buf;
1029  }
1030  */
1031 
1032  char* buf = new char[ length+1 ];
1033  buf[0] = 0;
1034 
1035  if ( fread( buf, length, 1, file ) != 1 ) {
1036  delete [] buf;
1037  SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
1038  return false;
1039  }
1040 
1041  // Process the buffer in place to normalize new lines. (See comment above.)
1042  // Copies from the 'p' to 'q' pointer, where p can advance faster if
1043  // a newline-carriage return is hit.
1044  //
1045  // Wikipedia:
1046  // Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or
1047  // CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A)...
1048  // * LF: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
1049  // * 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
1050  // * CR: Commodore 8-bit machines, Apple II family, Mac OS up to version 9 and OS-9
1051 
1052  const char* p = buf; // the read head
1053  char* q = buf; // the write head
1054  const char CR = 0x0d;
1055  const char LF = 0x0a;
1056 
1057  buf[length] = 0;
1058  while( *p ) {
1059  assert( p < (buf+length) );
1060  assert( q <= (buf+length) );
1061  assert( q <= p );
1062 
1063  if ( *p == CR ) {
1064  *q++ = LF;
1065  p++;
1066  if ( *p == LF ) { // check for CR+LF (and skip LF)
1067  p++;
1068  }
1069  }
1070  else {
1071  *q++ = *p++;
1072  }
1073  }
1074  assert( q <= (buf+length) );
1075  *q = 0;
1076 
1077  Parse( buf, 0, encoding );
1078 
1079  delete [] buf;
1080  return !Error();
1081 }
bool Error() const
Definition: tinyxml.h:1460
virtual const char * Parse(const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
void Clear()
Delete all the children of this node. Does not affect &#39;this&#39;.
Definition: tinyxml.cpp:169
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 704 of file tinyxmlparser.cpp.

705 {
706  ClearError();
707 
708  // Parse away, at the document level. Since a document
709  // contains nothing but other tags, most of what happens
710  // here is skipping white space.
711  if ( !p || !*p )
712  {
713  SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
714  return 0;
715  }
716 
717  // Note that, for a document, this needs to come
718  // before the while space skip, so that parsing
719  // starts from the pointer we are given.
720  location.Clear();
721  if ( prevData )
722  {
723  location.row = prevData->cursor.row;
724  location.col = prevData->cursor.col;
725  }
726  else
727  {
728  location.row = 0;
729  location.col = 0;
730  }
731  TiXmlParsingData data( p, TabSize(), location.row, location.col );
732  location = data.Cursor();
733 
734  if ( encoding == TIXML_ENCODING_UNKNOWN )
735  {
736  // Check for the Microsoft UTF-8 lead bytes.
737  const unsigned char* pU = (const unsigned char*)p;
738  if ( *(pU+0) && *(pU+0) == TIXML_UTF_LEAD_0
739  && *(pU+1) && *(pU+1) == TIXML_UTF_LEAD_1
740  && *(pU+2) && *(pU+2) == TIXML_UTF_LEAD_2 )
741  {
742  encoding = TIXML_ENCODING_UTF8;
743  useMicrosoftBOM = true;
744  }
745  }
746 
747  p = SkipWhiteSpace( p, encoding );
748  if ( !p )
749  {
750  SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
751  return 0;
752  }
753 
754  while ( p && *p )
755  {
756  TiXmlNode* node = Identify( p, encoding );
757  if ( node )
758  {
759  p = node->Parse( p, &data, encoding );
760  LinkEndChild( node );
761  }
762  else
763  {
764  break;
765  }
766 
767  // Did we get encoding info?
768  if ( encoding == TIXML_ENCODING_UNKNOWN
769  && node->ToDeclaration() )
770  {
771  TiXmlDeclaration* dec = node->ToDeclaration();
772  const char* enc = dec->Encoding();
773  assert( enc );
774 
775  if ( *enc == 0 )
776  encoding = TIXML_ENCODING_UTF8;
777  else if ( StringEqual( enc, "UTF-8", true, TIXML_ENCODING_UNKNOWN ) )
778  encoding = TIXML_ENCODING_UTF8;
779  else if ( StringEqual( enc, "UTF8", true, TIXML_ENCODING_UNKNOWN ) )
780  encoding = TIXML_ENCODING_UTF8; // incorrect, but be nice
781  else
782  encoding = TIXML_ENCODING_LEGACY;
783  }
784 
785  p = SkipWhiteSpace( p, encoding );
786  }
787 
788  // Was this empty?
789  if ( !firstChild ) {
790  SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, encoding );
791  return 0;
792  }
793 
794  // All is well.
795  return p;
796 }
void ClearError()
Definition: tinyxml.h:1511
TiXmlNode * LinkEndChild(TiXmlNode *addThis)
Definition: tinyxml.cpp:186
const char * Encoding() const
Encoding. Will return an empty string if none was found.
Definition: tinyxml.h:1311
virtual const TiXmlDeclaration * ToDeclaration() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:702
void TiXmlDocument::Print ( ) const
inline

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

Definition at line 1519 of file tinyxml.h.

1519 { Print( stdout, 0 ); }
void Print() const
Definition: tinyxml.h:1519
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 1452 of file tinyxml.h.

1452 { return FirstChildElement(); }
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:452
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 1504 of file tinyxml.h.

1504 { tabsize = _tabsize; }

The documentation for this class was generated from the following files: