TiXmlNode类继承自TiXmlBase类,而TiXmlNode类则是其他类的基类,比如TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlTest和TiXmlUnknown等。他们之间继承关系如下:
TiXmlNode是有类型的,比如element、text类型等:
enum NodeType
{
TINYXML_DOCUMENT,
TINYXML_ELEMENT,
TINYXML_COMMENT,
TINYXML_UNKNOWN,
TINYXML_TEXT,
TINYXML_DECLARATION,
TINYXML_TYPECOUNT
};
当进行XML文件操作时,实际上使用的是TiXmlNode指针,这样就运用了多态,对于不同的类型的节点(element、comment、text等),进行不同的操作。因为tinyxml把整个XML构建成了一个树形结构,并使用多态,所以速度上会有所减慢。
TiXMLNode类图
TiXMLNode定义在tinyxml.h文件中,以下是其UML类图
TiXmlNode的源码..
/** The parent class for everything in the Document Object Model.
(Except for attributes).
Nodes have siblings, a parent, and children. A node can be
in a document, or stand on its own. The type of a TiXmlNode
can be queried, and it can be cast to its more defined type.能够转化为更多定义的类型
*/
class TiXmlNode : public TiXmlBase
{
friend class TiXmlDocument;
friend class TiXmlElement;
public:
#ifdef TIXML_USE_STL
/** An input stream operator, for every class. Tolerant of newlines and
formatting, but doesn't expect them.
*/
friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
/** An output stream operator, for every class. Note that this outputs
without any newlines or formatting, as opposed to Print(), which
includes tabs and new lines.
The operator<< and operator>> are not completely symmetric. Writing
a node to a stream is very well defined. You'll get a nice stream
of output, without any extra whitespace or newlines.
But reading is not as well defined. (As it always is.) If you create
a TiXmlElement (for example) and read that from an input stream,
the text needs to define an element or junk will result. This is
true of all input streams, but it's worth keeping in mind.
A TiXmlDocument will read nodes until it reads a root element, and
all the children of that root element.
*/
friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
/// Appends the XML node or attribute to a std::string.
friend std::string& operator<< (std::string& out, const TiXmlNode& base );
#endif
/** The types of XML nodes supported by TinyXml. (All the
unsupported types are picked up by UNKNOWN.)
*/
enum NodeType
{
TINYXML_DOCUMENT,
TINYXML_ELEMENT,
TINYXML_COMMENT,
TINYXML_UNKNOWN,
TINYXML_TEXT,
TINYXML_DECLARATION,
TINYXML_TYPECOUNT
};
virtual ~TiXmlNode();
/** The meaning of 'value' changes for the specific type of
TiXmlNode.
@verbatim
Document: filename of the xml file
Element: name of the element
Comment: the comment text
Unknown: the tag contents
Text: the text string
@endverbatim
The subclasses will wrap this function.
*/
const char *Value() const { return value.c_str (); }
#ifdef TIXML_USE_STL
/** Return Value() as a std::string. If you only use STL,
this is more efficient than calling Value().
Only available in STL mode.
*/
const std::string& ValueStr() const { return value; }
#endif
const TIXML_STRING&