解析 tinyXML2 中的注释
parsing comment in tinyXML2
我在解析XML注释时遇到问题。如何正确访问评论?或者甚至可以用tinyXML2阅读注释吗?
<xml>
<foo> Text <!-- COMMENT --> <foo2/></foo>
</xml>
我创造了 XMLElement *root = xmlDoc->FirstChildElement("foo");
XMLElement *child = root->FirstChildElement();
子元素我得到 foo2 元素,什么是从文件中读取注释元素的 propper 方法。
谢谢
您可以使用
XMLNode::FirstChild()
和XMLNode::NextSibling()
遍历所有子节点。使用 dynamic_cast
测试 node 是否为注释。
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();
}
}
}
我只是通过阅读文档来弥补这一点,因此代码中可能存在错误。
我刚刚在我的项目上创建了一个函数,可以递归地导航整个文档并删除注释。您可以使用它来查看如何获取对文档的任何评论...效仿上面那个家伙的例子..
代码波纹管:
// Recursively navigates the XML and get rid of comments.
void StripXMLInfo(tinyxml2::XMLNode* node)
{
// All XML nodes may have children and siblings. So for each valid node, first we
// iterate on it's (possible) children, and then we proceed to clear the node itself and jump
// to the next sibling
while (node)
{
if (node->FirstChild() != NULL)
StripXMLInfo(node->FirstChild());
//Check to see if current node is a comment
auto comment = dynamic_cast<tinyxml2::XMLComment*>(node);
if (comment)
{
// If it is, we ask the parent to delete this, but first move pointer to next member so we don't get lost in a NULL reference
node = node->NextSibling();
comment->Parent()->DeleteChild(comment);
}
else
node = node->NextSibling();
}
}
相关文章:
- Visual Studio 2019:插入多个C++风格的单行注释
- 如何使用tinyxml2从XML加载父实体和子实体
- VSCode 中带有 C/C++ 扩展名的多行注释缩进错误
- 如果我注释掉换行符,为什么'string'会成为一个不合格的变量
- 为什么TinyXML2的XMLDocument::FirstChild()函数在尝试解析这个有效的XML文件时返回NULL?
- 为什么 ## aka 令牌粘贴运算符不适用于 C 和 C++ 中的注释?
- 如何让用户在 tinyxml2 中输入 XML 文件的路径
- 使用 TinyXML2 读取 C++ 中的 XML 文件
- 在 // C++注释中使用 \\ 是否合法?(C++评论中的LaTeX方程)
- 注释一行使代码工作,而没有它,代码不起作用
- 使用 Doxygen 在不同文件中注释函数
- 如何设置叮当格式的注释编译指示,以免触及多行doxygen注释?
- Qt - 带有注释的 JSON
- Visual Studio Community 代码分析的质量与 SAL 注释
- 如何阻止 ReSharper 在 C++ 中格式化多行注释
- C++,在多行代码段中注释
- 使用 Python 和正则表达式提取源代码中的C++注释
- 使用 C++ std::sregex_token_iterator 提取 HTML 注释
- C(嵌入式):注释 FreeRTOS 的 RootTask 时代码大小不会缩小
- 解析 tinyXML2 中的注释