📄 tinyxmla.h
字号:
friend class TiXmlElementA;public: /// Constructor. TiXmlTextA (const char * initValue) : TiXmlNodeA (TiXmlNodeA::TEXT) { SetValue( initValue ); } virtual ~TiXmlTextA() {} #ifdef TIXMLA_USE_STL /// Constructor. TiXmlTextA( const std::string& initValue ) : TiXmlNodeA (TiXmlNodeA::TEXT) { SetValue( initValue ); } #endif // [internal use] virtual void Print( FILE* cfile, int depth ) const;protected : // [internal use] Creates a new Element and returns it. virtual TiXmlNodeA* Clone() const; virtual void StreamOut ( TIXMLA_OSTREAM * out ) const; // [internal use] bool Blank() const; // returns true if all white space and new lines /* [internal use] Attribtue parsing starts: First char of the text returns: next char past '>' */ virtual const char* Parse( const char* p, TiXmlParsingDataA* data ); // [internal use] #ifdef TIXMLA_USE_STL virtual void StreamIn( TIXMLA_ISTREAM * in, TIXMLA_STRING * tag ); #endif};/** In correct XML the declaration is the first entry in the file. @verbatim <?xml version="1.0" standalone="yes"?> @endverbatim TinyXml will happily read or write files without a declaration, however. There are 3 possible attributes to the declaration: version, encoding, and standalone. Note: In this version of the code, the attributes are handled as special cases, not generic attributes, simply because there can only be at most 3 and they are always the same.*/class TiXmlDeclarationA : public TiXmlNodeA{public: /// Construct an empty declaration. TiXmlDeclarationA() : TiXmlNodeA( TiXmlNodeA::DECLARATION ) {}#ifdef TIXMLA_USE_STL /// Constructor. TiXmlDeclarationA( const std::string& _version, const std::string& _encoding, const std::string& _standalone ) : TiXmlNodeA( TiXmlNodeA::DECLARATION ) { version = _version; encoding = _encoding; standalone = _standalone; }#endif /// Construct. TiXmlDeclarationA( const char* _version, const char* _encoding, const char* _standalone ); virtual ~TiXmlDeclarationA() {} /// Version. Will return empty if none was found. const char * Version() const { return version.c_str (); } /// Encoding. Will return empty if none was found. const char * Encoding() const { return encoding.c_str (); } /// Is this a standalone document? const char * Standalone() const { return standalone.c_str (); } // [internal use] Creates a new Element and returs it. virtual TiXmlNodeA* Clone() const; // [internal use] virtual void Print( FILE* cfile, int depth ) const;protected: // used to be public #ifdef TIXMLA_USE_STL virtual void StreamIn( TIXMLA_ISTREAM * in, TIXMLA_STRING * tag ); #endif virtual void StreamOut ( TIXMLA_OSTREAM * out) const; // [internal use] // Attribtue parsing starts: next char past '<' // returns: next char past '>' virtual const char* Parse( const char* p, TiXmlParsingDataA* data );private: TIXMLA_STRING version; TIXMLA_STRING encoding; TIXMLA_STRING standalone;};/** Any tag that tinyXml doesn't recognize is saved as an unknown. It is a tag of text, but should not be modified. It will be written back to the XML, unchanged, when the file is saved.*/class TiXmlUnknownA : public TiXmlNodeA{public: TiXmlUnknownA() : TiXmlNodeA( TiXmlNodeA::UNKNOWN ) {} virtual ~TiXmlUnknownA() {} // [internal use] virtual TiXmlNodeA* Clone() const; // [internal use] virtual void Print( FILE* cfile, int depth ) const;protected: #ifdef TIXMLA_USE_STL virtual void StreamIn( TIXMLA_ISTREAM * in, TIXMLA_STRING * tag ); #endif virtual void StreamOut ( TIXMLA_OSTREAM * out ) const; /* [internal use] Attribute parsing starts: First char of the text returns: next char past '>' */ virtual const char* Parse( const char* p, TiXmlParsingDataA* data );};/** 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.*/class TiXmlDocumentA : public TiXmlNodeA{public: /// Create an empty document, that has no name. TiXmlDocumentA(); /// Create a document with a name. The name of the document is also the filename of the xml. TiXmlDocumentA( const char * documentName ); #ifdef TIXMLA_USE_STL /// Constructor. TiXmlDocumentA( const std::string& documentName ) : TiXmlNodeA( TiXmlNodeA::DOCUMENT ) { value = documentName; error = false; } #endif virtual ~TiXmlDocumentA() {} /** Load a file using the current document value. Returns true if successful. Will delete any existing document data before loading. */ bool LoadFile(); /// Save a file using the current document value. Returns true if successful. bool SaveFile() const; /// Load a file using the given filename. Returns true if successful. bool LoadFile( const char * filename ); /// Save a file using the given filename. Returns true if successful. bool SaveFile( const char * filename ) const; bool LoadUnicodeFilePath(const TCHAR* filename); #ifdef TIXMLA_USE_STL bool LoadFile( const std::string& filename ) ///< STL std::string version. { StringToBuffer f( filename ); return ( f.buffer && LoadFile( f.buffer )); } bool SaveFile( const std::string& filename ) const ///< STL std::string version. { StringToBuffer f( filename ); return ( f.buffer && SaveFile( f.buffer )); } #endif /** Parse the given null terminated block of xml data. */ virtual const char* Parse( const char* p, TiXmlParsingDataA* data = 0 ); /** 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. */ TiXmlElementA* RootElement() const { return FirstChildElement(); } /** 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) */ bool Error() const { return error; } /// Contains a textual (english) description of the error if one occurs. const char * ErrorDesc() const { return errorDesc.c_str (); } /** Generally, you probably want the error string ( ErrorDesc() ). But if you prefer the ErrorId, this function will fetch it. */ const int ErrorId() const { return errorId; } /** 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.) @sa SetTabSize, Row, Column */ int ErrorRow() { return errorLocation.row+1; } int ErrorCol() { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow() /** 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: @verbatim TiXmlDocumentA doc; doc.SetTabSize( 8 ); doc.Load( "myfile.xml" ); @endverbatim @sa Row, Column */ void SetTabSize( int _tabsize ) { tabsize = _tabsize; } int TabSize() const { return tabsize; } /** 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. */ void ClearError() { error = false; errorId = 0; errorDesc = ""; errorLocation.row = errorLocation.col = 0; //errorLocation.last = 0; } /** Dump the document to standard out. */ void Print() const { Print( stdout, 0 ); } // [internal use] virtual void Print( FILE* cfile, int depth = 0 ) const; // [internal use] void SetError( int err, const char* errorLocation, TiXmlParsingDataA* prevData );protected : virtual void StreamOut ( TIXMLA_OSTREAM * out) const; // [internal use] virtual TiXmlNodeA* Clone() const; #ifdef TIXMLA_USE_STL virtual void StreamIn( TIXMLA_ISTREAM * in, TIXMLA_STRING * tag ); #endifprivate: bool error; int errorId; TIXMLA_STRING errorDesc; int tabsize; TiXmlCursorA errorLocation;};/** A TiXmlHandleA is a class that wraps a node pointer with null checks; this is an incredibly useful thing. Note that TiXmlHandleA is not part of the TinyXml DOM structure. It is a separate utility class. Take an example: @verbatim <Document> <Element attributeA = "valueA"> <Child attributeB = "value1" /> <Child attributeB = "value2" /> </Element> <Document> @endverbatim Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very easy to write a *lot* of code that looks like: @verbatim TiXmlElementA* root = document.FirstChildElement( "Document" ); if ( root ) { TiXmlElementA* element = root->FirstChildElement( "Element" ); if ( element ) { TiXmlElementA* child = element->FirstChildElement( "Child" ); if ( child ) { TiXmlElementA* child2 = child->NextSiblingElement( "Child" ); if ( child2 ) { // Finally do something useful. @endverbatim And that doesn't even cover "else" cases. TiXmlHandleA addresses the verbosity of such code. A TiXmlHandleA checks for null pointers so it is perfectly safe and correct to use: @verbatim TiXmlHandleA docHandle( &document ); TiXmlElementA* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).Element(); if ( child2 ) { // do something useful @endverbatim Which is MUCH more concise and useful. It is also safe to copy handles - internally they are nothing more than node pointers. @verbatim TiXmlHandleA handleCopy = handle; @endverbatim What they should not be used for is iteration: @verbatim int i=0; while ( true ) { TiXmlElementA* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).Element(); if ( !child ) break; // do something ++i; } @endverbatim It seems reasonable, but it is in fact two embedded while loops. The Child method is a linear walk to find the element, so this code would iterate much more than it needs to. Instead, prefer: @verbatim TiXmlElementA* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).Element(); for( child; child; child=child->NextSiblingElement() ) { // do something } @endverbatim*/class TiXmlHandleA{public: /// Create a handle from any node (at any depth of the tree.) This can be a null pointer. TiXmlHandleA( TiXmlNodeA* node ) { this->node = node; } /// Copy constructor TiXmlHandleA( const TiXmlHandleA& ref ) { this->node = ref.node; } /// Return a handle to the first child node. TiXmlHandleA FirstChild() const; /// Return a handle to the first child node with the given name. TiXmlHandleA FirstChild( const char * value ) const; /// Return a handle to the first child element. TiXmlHandleA FirstChildElement() const; /// Return a handle to the first child element with the given name. TiXmlHandleA FirstChildElement( const char * value ) const; /** Return a handle to the "index" child with the given name. The first child is 0, the second 1, etc. */ TiXmlHandleA Child( const char* value, int index ) const; /** Return a handle to the "index" child. The first child is 0, the second 1, etc. */ TiXmlHandleA Child( int index ) const; /** Return a handle to the "index" child element with the given name. The first child element is 0, the second 1, etc. Note that only TiXmlElements are indexed: other types are not counted. */ TiXmlHandleA ChildElement( const char* value, int index ) const; /** Return a handle to the "index" child element. The first child element is 0, the second 1, etc. Note that only TiXmlElements are indexed: other types are not counted. */ TiXmlHandleA ChildElement( int index ) const; #ifdef TIXMLA_USE_STL TiXmlHandleA FirstChild( const std::string& _value ) const { return FirstChild( _value.c_str() ); } TiXmlHandleA FirstChildElement( const std::string& _value ) const { return FirstChildElement( _value.c_str() ); } TiXmlHandleA Child( const std::string& _value, int index ) const { return Child( _value.c_str(), index ); } TiXmlHandleA ChildElement( const std::string& _value, int index ) const { return ChildElement( _value.c_str(), index ); } #endif /// Return the handle as a TiXmlNodeA. This may return null. TiXmlNodeA* Node() const { return node; } /// Return the handle as a TiXmlElementA. This may return null. TiXmlElementA* Element() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } /// Return the handle as a TiXmlTextA. This may return null. TiXmlTextA* Text() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }private: TiXmlNodeA* node;};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -