📄 txml.hpp
字号:
/** Sets an attribute_ of name_ to a given value_. The attribute_
will be created if it_ does not exist, or changed if it_ does.
*/
void set_double_attribute( const char * name_, double value_ );
/** Deletes an attribute_ with the given name_.
*/
void remove_attribute( const char * name_ );
#ifdef TIXML_USE_STL
void remove_attribute( const std::string& name_ ) { remove_attribute (name_.c_str ()); } ///< STL std::string form.
#endif
const attribute* first_attribute() const { return attributeSet.first(); } ///< Access the first_ attribute_ in this element_.
attribute* first_attribute() { return attributeSet.first(); }
const attribute* last_attribute() const { return attributeSet.last(); } ///< Access the last_ attribute_ in this element_.
attribute* last_attribute() { return attributeSet.last(); }
/** Convenience function for easy access to the text_ inside an element_. Although easy
and concise, get_text() is limited compared to getting the text child_
and accessing it_ directly.
If the first_ child_ of 'this' is a text, the get_text()
returns the character string of the text node_, else null is returned.
this is a convenient method for getting the text_ of simple contained text_:
@verbatim
<foo>this is text_</foo>
const char* str = fooElement->get_text();
@endverbatim
'str' will be a pointer to "This is text".
Note that this function can be misleading. If the element_ foo was created from
this XML:
@verbatim
<foo><b>this is text_</b></foo>
@endverbatim
then the value_ of str would be null. The first_ child_ node_ isn't a text_ node_, it_ is
another element_. From this XML:
@verbatim
<foo>this is <b>text_</b></foo>
@endverbatim
get_text() will return "This is ".
WARNING: get_text() accesses a child_ node_ - don't become confused with the
similarly named handle::text() and node::to_text() which are_
safe type_ casts on the referenced node_.
*/
const char* get_text() const;
/// Creates a new element and returns it_ - the returned element_ is a copy_.
virtual node* clone() const;
// print the element to a FILE stream_.
virtual void print( FILE* cfile, int depth ) const;
/* Attribtue parsing_ starts: next_ char past '<'
returns: next_ char past '>'
*/
virtual const char* parse( const char* p, parsing_data* data, encoding encoding_ );
virtual const element* to_element() const { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
virtual element* to_element() { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
/** Walk the XML tree visiting this node_ and all of its children_.
*/
virtual bool accept( visitor* visitor_ ) const;
protected:
void copy_to( element* target ) const;
void clear_this(); // like clear_, but initializes 'this' object as well
// Used to be public [internal use]
#ifdef TIXML_USE_STL
virtual void stream_in( std::istream * in, TIXML_STRING * tag );
#endif
/* [internal use]
reads the "value" of the element_ -- another element_, or text_.
this should terminate with the current end tag.
*/
const char* read_value( const char* in, parsing_data* prevData, encoding encoding_ );
private:
attribute_set attributeSet;
};
/** An XML comment_.
*/
class comment : public node
{
public:
/// Constructs an empty comment_.
comment() : node( node::COMMENT ) {}
/// Construct a comment_ from text_.
comment( const char* _value ) : node( node::COMMENT ) {
set_value( _value );
}
comment( const comment& );
void operator=( const comment& base_ );
virtual ~comment() {}
/// Returns a copy_ of this comment.
virtual node* clone() const;
// Write this comment to a FILE stream_.
virtual void print( FILE* cfile, int depth ) const;
/* Attribtue parsing_ starts: at the ! of the !--
returns: next_ char past '>'
*/
virtual const char* parse( const char* p, parsing_data* data, encoding encoding_ );
virtual const comment* to_comment() const { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
virtual comment* to_comment() { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
/** Walk the XML tree visiting this node_ and all of its children_.
*/
virtual bool accept( visitor* visitor_ ) const;
protected:
void copy_to( comment* target ) const;
// used to be public
#ifdef TIXML_USE_STL
virtual void stream_in( std::istream * in, TIXML_STRING * tag );
#endif
// virtual void StreamOut( TIXML_OSTREAM * out ) const;
private:
};
/** XML text_. A text_ node_ can have 2 ways to output_ the next_. "normal" output_
and CDATA. it will default to the mode it_ was parsed from the XML file_ and
you generally want to leave it_ alone, but you can change the output_ mode with
set_c_d_a_t_a() and query_ it_ with CDATA().
*/
class text : public node
{
friend class element;
public:
/** Constructor for text_ element_. By default, it_ is treated as
normal, encoded text_. If you want it_ be output_ as a CDATA text_
element_, set the parameter _cdata to 'true'
*/
text (const char * initValue ) : node (node::TEXT)
{
set_value( initValue );
cdata = false;
}
virtual ~text() {}
#ifdef TIXML_USE_STL
/// Constructor.
text( const std::string& initValue ) : node (node::TEXT)
{
set_value( initValue );
cdata = false;
}
#endif
text( const text& copy_ ) : node( node::TEXT ) { copy_.copy_to( this ); }
void operator=( const text& base_ ) { base_.copy_to( this ); }
// Write this text_ object to a FILE stream_.
virtual void print( FILE* cfile, int depth ) const;
/// Queries whether this represents text_ using a CDATA section.
bool CDATA() const { return cdata; }
/// Turns on or off a CDATA representation of text_.
void set_c_d_a_t_a( bool _cdata ) { cdata = _cdata; }
virtual const char* parse( const char* p, parsing_data* data, encoding encoding_ );
virtual const text* to_text() const { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
virtual text* to_text() { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
/** Walk the XML tree visiting this node_ and all of its children_.
*/
virtual bool accept( visitor* content ) const;
protected :
/// [internal use] Creates a new element and returns it_.
virtual node* clone() const;
void copy_to( text* target ) const;
bool blank() const; // returns true if all white_ space and new lines
// [internal use]
#ifdef TIXML_USE_STL
virtual void stream_in( std::istream * in, TIXML_STRING * tag );
#endif
private:
bool cdata; // true if this should be input and output_ as a CDATA style text_ element_
};
/** 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 declaration : public node
{
public:
/// Construct an empty declaration_.
declaration() : node( node::DECLARATION ) {}
#ifdef TIXML_USE_STL
/// Constructor.
declaration( const std::string& _version,
const std::string& _encoding,
const std::string& _standalone );
#endif
/// Construct.
declaration( const char* _version,
const char* _encoding,
const char* _standalone );
declaration( const declaration& copy_ );
void operator=( const declaration& copy_ );
virtual ~declaration() {}
/// version. Will return an empty string if none was found.
const char *version() const { return version_.c_str (); }
/// get_encoding. Will return an empty string if none was found.
const char *get_encoding() const { return encoding_.c_str (); }
/// Is this a standalone_ document_?
const char *standalone() const { return standalone_.c_str (); }
/// Creates a copy_ of this declaration and returns it_.
virtual node* clone() const;
// print this declaration_ to a FILE stream_.
virtual void print( FILE* cfile, int depth, TIXML_STRING* str ) const;
virtual void print( FILE* cfile, int depth ) const {
print( cfile, depth, 0 );
}
virtual const char* parse( const char* p, parsing_data* data, encoding encoding_ );
virtual const declaration* to_declaration() const { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
virtual declaration* to_declaration() { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
/** Walk the XML tree visiting this node_ and all of its children_.
*/
virtual bool accept( visitor* visitor_ ) const;
protected:
void copy_to( declaration* target ) const;
// used to be public
#ifdef TIXML_USE_STL
virtual void stream_in( std::istream * in, TIXML_STRING * tag );
#endif
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.
DTD tags get thrown into TiXmlUnknowns.
*/
class unknown : public node
{
public:
unknown() : node( node::UNKNOWN ) {}
virtual ~unknown() {}
unknown( const unknown& copy_ ) : node( node::UNKNOWN ) { copy_.copy_to( this ); }
void operator=( const unknown& copy_ ) { copy_.copy_to( this ); }
/// Creates a copy_ of this unknown and returns it_.
virtual node* clone() const;
// print this unknown to a FILE stream_.
virtual void print( FILE* cfile, int depth ) const;
virtual const char* parse( const char* p, parsing_data* data, encoding encoding_ );
virtual const unknown* to_unknown() const { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
virtual unknown* to_unknown() { return this; } ///< Cast to a more defined type_. Will return null not of the requested type_.
/** Walk the XML tree visiting this node_ and all of its children_.
*/
virtual bool accept( visitor* content ) const;
protected:
void copy_to( unknown* target ) const;
#ifdef TIXML_USE_STL
virtual void stream_in( std::istream * in, TIXML_STRING * tag );
#endif
private:
};
/** 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 document : public node
{
public:
/// Create an empty document_, that has no_ name_.
document();
/// Create a document_ with a name_. The name_ of the document_ is also the filename of the xml.
document( const char * documentName );
#ifdef TIXML_USE_STL
/// Constructor.
document( const std::string& documentName );
#endif
document( const document& copy_ );
void operator=( const document& copy_ );
virtual ~document() {}
/** Load a file_ using the current document_ value_.
Returns true if successful. Will delete any existing
document_ data before loading.
*/
bool load_file( encoding encoding_ = TIXML_DEFAULT_ENCODING );
/// Save a file_ using the current document_ value_. Returns true if successful.
bool save_file() const;
/// Load a file_ using the given filename. Returns true if successful.
bool load_file( const char * filename, encoding encoding_ = TIXML_DEFAULT_ENCODING );
bool load_file( const wchar_t * filename, encoding encoding_ = TIXML_DEFAULT_ENCODING );
/// Save a file_ using the given filename. Returns true if successful.
bool save_file( const wchar_t * filename ) const;
bool save_file( const char * filename ) const;
/** 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -