【问题标题】:How to count elements in TinyXml?如何计算 TinyXml 中的元素?
【发布时间】:2023-03-12 17:38:01
【问题描述】:

我认为问题在于使用函数或其他错误。

这部分代码可以运行,但是效果不好。

TiXmlElement* e = hDoc.FirstChildElement().Element(); // think problem is there
while (e)
{
    e = e->NextSiblingElement();  //or may be there
    count++;
}

count的结果是1。


Xml 文件是:

<doc>
   <state> ... </state>
   <state> ... </state>
   ...
</doc>

找不到工作示例。

【问题讨论】:

  • 结果有什么“不好”的地方?你得到什么结果,你期望什么结果?
  • 然后删除问题或将解决方案作为答案发布。
  • 我解决了:第一行 TiXmlElement* e = hDoc.FirstChildElement().FirstChildElement().Element(); 得到 的计数

标签: c++ xml-parsing tinyxml


【解决方案1】:

如果您阅读documentation,您可以找到以下示例(这似乎比您的方法更简洁):

for( child = parent->FirstChild(); child; child = child->NextSibling() )
    count++;

但您可能只是想计算州,所以我建议:

for( child = parent->FirstChild("state"); child; child = child->NextSibling("state") )

您可能还想要这样的东西:

TiXmlElement *parent = hDoc.RootElement();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多