我在解析XML注释时遇到了问题。我怎样才能正确地访问评论?或者甚至可以用tinyXML2阅读评论?
<xml>
<foo> Text <!-- COMMENT --> <foo2/></foo>
</xml>我创建了XMLElement *root = xmlDoc->FirstChildElement("foo"); XMLElement *child = root->FirstChildElement();
从子元素中,我得到了foo2元素,这是从文件中读取注释元素的更合适的方式。
谢谢
发布于 2017-04-09 21:53:14
您可以使用XMLNode::FirstChild()和XMLNode::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();
}
}
}这是我在阅读documentation时编造的,所以代码中可能有错误。
https://stackoverflow.com/questions/43305359
复制相似问题