0

我在解析 XML 注释时遇到问题。我怎样才能正确访问评论?或者甚至可以用 tinyXML2 阅读评论?

<xml>
<foo> Text <!-- COMMENT --> <foo2/></foo>
</xml>

我建立 XMLElement *root = xmlDoc->FirstChildElement("foo"); XMLElement *child = root->FirstChildElement();

从子元素我得到 foo2 元素,从文件中读取注释元素的正确方法是什么。

谢谢

4

1 回答 1

1

您可以使用XMLNode::FirstChild()andXMLNode::NextSibling()循环遍历所有子节点。用于dynamic_cast测试节点是否是评论。

if( const XMLElement *root = xmlDoc->FirstChildElement("foo") )
{
    for( const XMLNode* node = root->FirstChild(); node; node = node->NextSibling() )
    {
        if( auto comment = dynamic_cast<const XMLComment*>( node ) )
        {
            const char* commentText = comment->Value();
        }   
    }
}

我只是通过阅读文档来弥补这一点,因此代码中可能存在错误。

于 2017-04-09T13:53:13.537 回答