📄 tinyxml.h
字号:
class DLLIMPORT TiXmlText : public TiXmlNode
{
friend class TiXmlElement;
public:
/// Constructor.
TiXmlText (const char * initValue) : TiXmlNode (TiXmlNode::TEXT)
{
SetValue( initValue );
}
virtual ~TiXmlText() {}
#ifdef TIXML_USE_STL
/// Constructor.
TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::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 TiXmlNode* Clone() const;
virtual void StreamOut ( TIXML_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, TiXmlParsingData* data );
// [internal use]
#ifdef TIXML_USE_STL
virtual void StreamIn( TIXML_ISTREAM * in, TIXML_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 DLLIMPORT TiXmlDeclaration : public TiXmlNode
{
public:
/// Construct an empty declaration.
TiXmlDeclaration() : TiXmlNode( TiXmlNode::DECLARATION ) {}
#ifdef TIXML_USE_STL
/// Constructor.
TiXmlDeclaration( const std::string& _version,
const std::string& _encoding,
const std::string& _standalone )
: TiXmlNode( TiXmlNode::DECLARATION )
{
version = _version;
encoding = _encoding;
standalone = _standalone;
}
#endif
/// Construct.
TiXmlDeclaration( const char* _version,
const char* _encoding,
const char* _standalone );
virtual ~TiXmlDeclaration() {}
/// 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 TiXmlNode* Clone() const;
// [internal use]
virtual void Print( FILE* cfile, int depth ) const;
protected:
// used to be public
#ifdef TIXML_USE_STL
virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
#endif
virtual void StreamOut ( TIXML_OSTREAM * out) const;
// [internal use]
// Attribtue parsing starts: next char past '<'
// returns: next char past '>'
virtual const char* Parse( const char* p, TiXmlParsingData* data );
private:
TIXML_STRING version;
TIXML_STRING encoding;
TIXML_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 DLLIMPORT TiXmlUnknown : public TiXmlNode
{
public:
TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN ) {}
virtual ~TiXmlUnknown() {}
// [internal use]
virtual TiXmlNode* Clone() const;
// [internal use]
virtual void Print( FILE* cfile, int depth ) const;
protected:
#ifdef TIXML_USE_STL
virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
#endif
virtual void StreamOut ( TIXML_OSTREAM * out ) const;
/* [internal use]
Attribute parsing starts: First char of the text
returns: next char past '>'
*/
virtual const char* Parse( const char* p, TiXmlParsingData* 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 DLLIMPORT TiXmlDocument : public TiXmlNode
{
public:
/// Create an empty document, that has no name.
TiXmlDocument();
/// Create a document with a name. The name of the document is also the filename of the xml.
TiXmlDocument( const char * documentName );
#ifdef TIXML_USE_STL
/// Constructor.
TiXmlDocument( const std::string& documentName ) :
TiXmlNode( TiXmlNode::DOCUMENT )
{
value = documentName;
error = false;
}
#endif
virtual ~TiXmlDocument() {}
/** 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;
#ifdef TIXML_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, TiXmlParsingData* 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.
*/
TiXmlElement* 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
TiXmlDocument 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, TiXmlParsingData* prevData );
protected :
virtual void StreamOut ( TIXML_OSTREAM * out) const;
// [internal use]
virtual TiXmlNode* Clone() const;
#ifdef TIXML_USE_STL
virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
#endif
private:
bool error;
int errorId;
TIXML_STRING errorDesc;
int tabsize;
TiXmlCursor errorLocation;
};
/**
A TiXmlHandle is a class that wraps a node pointer with null checks; this is
an incredibly useful thing. Note that TiXmlHandle 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
TiXmlElement* root = document.FirstChildElement( "Document" );
if ( root )
{
TiXmlElement* element = root->FirstChildElement( "Element" );
if ( element )
{
TiXmlElement* child = element->FirstChildElement( "Child" );
if ( child )
{
TiXmlElement* child2 = child->NextSiblingElement( "Child" );
if ( child2 )
{
// Finally do something useful.
@endverbatim
And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity
of such code. A TiXmlHandle checks for null pointers so it is perfectly safe
and correct to use:
@verbatim
TiXmlHandle docHandle( &document );
TiXmlElement* 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
TiXmlHandle handleCopy = handle;
@endverbatim
What they should not be used for is iteration:
@verbatim
int i=0;
while ( true )
{
TiXmlElement* 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
TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).Element();
for( child; child; child=child->NextSiblingElement() )
{
// do something
}
@endverbatim
*/
class DLLIMPORT TiXmlHandle
{
public:
/// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
TiXmlHandle( TiXmlNode* node ) { this->node = node; }
/// Copy constructor
TiXmlHandle( const TiXmlHandle& ref ) { this->node = ref.node; }
/// Return a handle to the first child node.
TiXmlHandle FirstChild() const;
/// Return a handle to the first child node with the given name.
TiXmlHandle FirstChild( const char * value ) const;
/// Return a handle to the first child element.
TiXmlHandle FirstChildElement() const;
/// Return a handle to the first child element with the given name.
TiXmlHandle 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.
*/
TiXmlHandle Child( const char* value, int index ) const;
/** Return a handle to the "index" child.
The first child is 0, the second 1, etc.
*/
TiXmlHandle 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.
*/
TiXmlHandle 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.
*/
TiXmlHandle ChildElement( int index ) const;
#ifdef TIXML_USE_STL
TiXmlHandle FirstChild( const std::string& _value ) const { return FirstChild( _value.c_str() ); }
TiXmlHandle FirstChildElement( const std::string& _value ) const { return FirstChildElement( _value.c_str() ); }
TiXmlHandle Child( const std::string& _value, int index ) const { return Child( _value.c_str(), index ); }
TiXmlHandle ChildElement( const std::string& _value, int index ) const { return ChildElement( _value.c_str(), index ); }
#endif
/// Return the handle as a TiXmlNode. This may return null.
TiXmlNode* Node() const { return node; }
/// Return the handle as a TiXmlElement. This may return null.
TiXmlElement* Element() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
/// Return the handle as a TiXmlText. This may return null.
TiXmlText* Text() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
private:
TiXmlNode* node;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -