什么是 tinyxml2 (v2) 替代 v1 的 TiXmlNode 枚举?
TinyXML v1 可以打开节点类型,但是如何使用 TinyXML v2 的 XMLNode 做同样的事情。
switch (node->Type()) // v1 node type selector
{
case TiXmlNode::DOCUMENT:
wcout << L"Hello Document";
break;
什么是 tinyxml2 (v2) 替代 v1 的 TiXmlNode 枚举?
TinyXML v1 可以打开节点类型,但是如何使用 TinyXML v2 的 XMLNode 做同样的事情。
switch (node->Type()) // v1 node type selector
{
case TiXmlNode::DOCUMENT:
wcout << L"Hello Document";
break;
基类XMLNode
包含许多虚拟转换方法,它们返回NULL
或0
除非节点实际上是指定的类型。
例如,如果你调用ToText()
一个实际上是 的东西XMLText
,你会得到一个有效的XMLText*
结果,否则你会得到NULL
.
以下是可用的方法(在 中XMLNode
):
/// Safely cast to an Element, or null.
virtual XMLElement* ToElement() {
return 0;
}
/// Safely cast to Text, or null.
virtual XMLText* ToText() {
return 0;
}
/// Safely cast to a Comment, or null.
virtual XMLComment* ToComment() {
return 0;
}
/// Safely cast to a Document, or null.
virtual XMLDocument* ToDocument() {
return 0;
}
/// Safely cast to a Declaration, or null.
virtual XMLDeclaration* ToDeclaration() {
return 0;
}
/// Safely cast to an Unknown, or null.
virtual XMLUnknown* ToUnknown() {
return 0;
}
我不确定为什么要这样做;也许类型枚举在实践中没有那么有用,或者它可能是为了支持内部XMLHandle
类(它实现了所有这些强制转换方法)。要转换您的代码,您可以从这里开始:
switch (node->Type()) {
case TiXMLNode::DOCUMENT:
...
break;
case TiXMLNode::TEXT:
...
break;
...
}
进入这个:
XMLDocument *doc = node->ToDocument();
if (doc) {
...
}
XMLText *text = node->ToText();
if (text) {
...
}