📄 tinyxml.h
字号:
NOTE: the node to be added is passed by pointer, and will be
henceforth owned (and deleted) by tinyXml. This method is efficient
and avoids an extra copy, but should be used with care as it
uses a different memory model than the other insert functions.
@sa InsertEndChild
*/
TiXmlNode* LinkEndChild( TiXmlNode* addThis );
/** Add a new node related to this. Adds a child before the specified child.
Returns a pointer to the new object or NULL if an error occured.
*/
TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
/** Add a new node related to this. Adds a child after the specified child.
Returns a pointer to the new object or NULL if an error occured.
*/
TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
/** Replace a child of this node.
Returns a pointer to the new object or NULL if an error occured.
*/
TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
/// Delete a child of this node.
bool RemoveChild( TiXmlNode* removeThis );
/// Navigate to a sibling node.
TiXmlNode* PreviousSibling() const { return prev; }
/// Navigate to a sibling node.
TiXmlNode* PreviousSibling( const char * ) const;
#ifdef TIXML_USE_STL
TiXmlNode* PreviousSibling( const std::string& _value ) const { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
TiXmlNode* NextSibling( const std::string& _value) const { return NextSibling (_value.c_str ()); } ///< STL std::string form.
#endif
/// Navigate to a sibling node.
TiXmlNode* NextSibling() const { return next; }
/// Navigate to a sibling node with the given 'value'.
TiXmlNode* NextSibling( const char * ) const;
/** Convenience function to get through elements.
Calls NextSibling and ToElement. Will skip all non-Element
nodes. Returns 0 if there is not another element.
*/
TiXmlElement* NextSiblingElement() const;
/** Convenience function to get through elements.
Calls NextSibling and ToElement. Will skip all non-Element
nodes. Returns 0 if there is not another element.
*/
TiXmlElement* NextSiblingElement( const char * ) const;
#ifdef TIXML_USE_STL
TiXmlElement* NextSiblingElement( const std::string& _value) const { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
#endif
/// Convenience function to get through elements.
TiXmlElement* FirstChildElement() const;
/// Convenience function to get through elements.
TiXmlElement* FirstChildElement( const char * value ) const;
#ifdef TIXML_USE_STL
TiXmlElement* FirstChildElement( const std::string& _value ) const { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
#endif
/** Query the type (as an enumerated value, above) of this node.
The possible types are: DOCUMENT, ELEMENT, COMMENT,
UNKNOWN, TEXT, and DECLARATION.
*/
virtual int Type() const { return type; }
/** Return a pointer to the Document this node lives in.
Returns null if not in a document.
*/
TiXmlDocument* GetDocument() const;
/// Returns true if this node has no children.
bool NoChildren() const { return !firstChild; }
TiXmlDocument* ToDocument() const { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
TiXmlElement* ToElement() const { return ( this && type == ELEMENT ) ? (TiXmlElement*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
TiXmlComment* ToComment() const { return ( this && type == COMMENT ) ? (TiXmlComment*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
TiXmlUnknown* ToUnknown() const { return ( this && type == UNKNOWN ) ? (TiXmlUnknown*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
TiXmlText* ToText() const { return ( this && type == TEXT ) ? (TiXmlText*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
TiXmlDeclaration* ToDeclaration() const { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
virtual TiXmlNode* Clone() const = 0;
void SetUserData( void* user ) { userData = user; }
void* GetUserData() { return userData; }
protected:
TiXmlNode( NodeType type );
#ifdef TIXML_USE_STL
// The real work of the input operator.
virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
#endif
// Figure out what is at *p, and parse it. Returns null if it is not an xml node.
TiXmlNode* Identify( const char* start );
void CopyToClone( TiXmlNode* target ) const { target->SetValue (value.c_str() );
target->userData = userData; }
// Internal Value function returning a TIXML_STRING
TIXML_STRING SValue() const { return value ; }
TiXmlNode* parent;
NodeType type;
TiXmlNode* firstChild;
TiXmlNode* lastChild;
TIXML_STRING value;
TiXmlNode* prev;
TiXmlNode* next;
void* userData;
};
/** An attribute is a name-value pair. Elements have an arbitrary
number of attributes, each with a unique name.
@note The attributes are not TiXmlNodes, since they are not
part of the tinyXML document object model. There are other
suggested ways to look at this problem.
*/
class DLLIMPORT TiXmlAttribute : public TiXmlBase
{
friend class TiXmlAttributeSet;
public:
/// Construct an empty attribute.
TiXmlAttribute()
{
document = 0;
prev = next = 0;
}
#ifdef TIXML_USE_STL
/// std::string constructor.
TiXmlAttribute( const std::string& _name, const std::string& _value )
{
name = _name;
value = _value;
document = 0;
prev = next = 0;
}
#endif
/// Construct an attribute with a name and value.
TiXmlAttribute( const char * _name, const char * _value )
{
name = _name;
value = _value;
document = 0;
prev = next = 0;
}
const char* Name() const { return name.c_str (); } ///< Return the name of this attribute.
const char* Value() const { return value.c_str (); } ///< Return the value of this attribute.
const int IntValue() const; ///< Return the value of this attribute, converted to an integer.
const double DoubleValue() const; ///< Return the value of this attribute, converted to a double.
/** QueryIntValue examines the value string. It is an alternative to the
IntValue() method with richer error checking.
If the value is an integer, it is stored in 'value' and
the call returns TIXML_SUCCESS. If it is not
an integer, it returns TIXML_WRONG_TYPE.
A specialized but useful call. Note that for success it returns 0,
which is the opposite of almost all other TinyXml calls.
*/
int QueryIntValue( int* value ) const;
/// QueryDoubleValue examines the value string. See QueryIntValue().
int QueryDoubleValue( double* value ) const;
void SetName( const char* _name ) { name = _name; } ///< Set the name of this attribute.
void SetValue( const char* _value ) { value = _value; } ///< Set the value.
void SetIntValue( int value ); ///< Set the value from an integer.
void SetDoubleValue( double value ); ///< Set the value from a double.
#ifdef TIXML_USE_STL
/// STL std::string form.
void SetName( const std::string& _name )
{
StringToBuffer buf( _name );
SetName ( buf.buffer ? buf.buffer : "error" );
}
/// STL std::string form.
void SetValue( const std::string& _value )
{
StringToBuffer buf( _value );
SetValue( buf.buffer ? buf.buffer : "error" );
}
#endif
/// Get the next sibling attribute in the DOM. Returns null at end.
TiXmlAttribute* Next() const;
/// Get the previous sibling attribute in the DOM. Returns null at beginning.
TiXmlAttribute* Previous() const;
bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
/* [internal use]
Attribtue parsing starts: first letter of the name
returns: the next char after the value end quote
*/
virtual const char* Parse( const char* p, TiXmlParsingData* data );
// [internal use]
virtual void Print( FILE* cfile, int depth ) const;
virtual void StreamOut( TIXML_OSTREAM * out ) const;
// [internal use]
// Set the document pointer so the attribute can report errors.
void SetDocument( TiXmlDocument* doc ) { document = doc; }
private:
TiXmlDocument* document; // A pointer back to a document, for error reporting.
TIXML_STRING name;
TIXML_STRING value;
TiXmlAttribute* prev;
TiXmlAttribute* next;
};
/* A class used to manage a group of attributes.
It is only used internally, both by the ELEMENT and the DECLARATION.
The set can be changed transparent to the Element and Declaration
classes that use it, but NOT transparent to the Attribute
which has to implement a next() and previous() method. Which makes
it a bit problematic and prevents the use of STL.
This version is implemented with circular lists because:
- I like circular lists
- it demonstrates some independence from the (typical) doubly linked list.
*/
class DLLIMPORT TiXmlAttributeSet
{
public:
TiXmlAttributeSet();
~TiXmlAttributeSet();
void Add( TiXmlAttribute* attribute );
void Remove( TiXmlAttribute* attribute );
TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
TiXmlAttribute* Find( const char * name ) const;
private:
TiXmlAttribute sentinel;
};
/** The element is a container class. It has a value, the element name,
and can contain other elements, text, comments, and unknowns.
Elements also contain an arbitrary number of attributes.
*/
class DLLIMPORT TiXmlElement : public TiXmlNode
{
public:
/// Construct an element.
TiXmlElement (const char * in_value);
#ifdef TIXML_USE_STL
/// std::string constructor.
TiXmlElement( const std::string& _value ) : TiXmlNode( TiXmlNode::ELEMENT )
{
firstChild = lastChild = 0;
value = _value;
}
#endif
virtual ~TiXmlElement();
/** Given an attribute name, Attribute() returns the value
for the attribute of that name, or null if none exists.
*/
const char* Attribute( const char* name ) const;
/** Given an attribute name, Attribute() returns the value
for the attribute of that name, or null if none exists.
If the attribute exists and can be converted to an integer,
the integer value will be put in the return 'i', if 'i'
is non-null.
*/
const char* Attribute( const char* name, int* i ) const;
/** Given an attribute name, Attribute() returns the value
for the attribute of that name, or null if none exists.
If the attribute exists and can be converted to an double,
the double value will be put in the return 'd', if 'd'
is non-null.
*/
const char* Attribute( const char* name, double* d ) const;
/** QueryIntAttribute examines the attribute - it is an alternative to the
Attribute() method with richer error checking.
If the attribute is an integer, it is stored in 'value' and
the call returns TIXML_SUCCESS. If it is not
an integer, it returns TIXML_WRONG_TYPE. If the attribute
does not exist, then TIXML_NO_ATTRIBUTE is returned.
*/
int QueryIntAttribute( const char* name, int* value ) const;
/// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
int QueryDoubleAttribute( const char* name, double* value ) const;
/** 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 SetAttribute( const char* name, const char * value );
#ifdef TIXML_USE_STL
const char* Attribute( const std::string& name ) const { return Attribute( name.c_str() ); }
const char* Attribute( const std::string& name, int* i ) const { return Attribute( name.c_str(), i ); }
/// STL std::string form.
void SetAttribute( const std::string& name, const std::string& _value )
{
StringToBuffer n( name );
StringToBuffer v( _value );
if ( n.buffer && v.buffer )
SetAttribute (n.buffer, v.buffer );
}
///< STL std::string form.
void SetAttribute( const std::string& name, int _value )
{
StringToBuffer n( name );
if ( n.buffer )
SetAttribute (n.buffer, _value);
}
#endif
/** 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 SetAttribute( const char * name, int value );
/** Deletes an attribute with the given name.
*/
void RemoveAttribute( const char * name );
#ifdef TIXML_USE_STL
void RemoveAttribute( const std::string& name ) { RemoveAttribute (name.c_str ()); } ///< STL std::string form.
#endif
TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); } ///< Access the first attribute in this element.
TiXmlAttribute* LastAttribute() const { return attributeSet.Last(); } ///< Access the last attribute in this element.
// [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 [internal use]
#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 );
/* [internal use]
Reads the "value" of the element -- another element, or text.
This should terminate with the current end tag.
*/
const char* ReadValue( const char* in, TiXmlParsingData* prevData );
private:
TiXmlAttributeSet attributeSet;
};
/** An XML comment.
*/
class DLLIMPORT TiXmlComment : public TiXmlNode
{
public:
/// Constructs an empty comment.
TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
virtual ~TiXmlComment() {}
// [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: at the ! of the !--
returns: next char past '>'
*/
virtual const char* Parse( const char* p, TiXmlParsingData* data );
};
/** XML text. Contained in an element.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -