使用 Tinyxml 检查 XML 节点是否存在

发布于 2024-10-31 13:25:15 字数 453 浏览 4 评论 0原文

我正在使用tinyXml 来解析C++ 中的XML 文件。谁能告诉我如何检查节点(父/子/下一个兄弟)是否存在。下面是我正在处理的 xml 文件中存在的唯一节点。

TiXmlElement* Instrmt = TrdCaptRpt->FirstChildElement();
TiXmlElement* Undly = Instrmt->NextSiblingElement();
TiXmlElement* Amt = Undly->NextSiblingElement();
TiXmlElement* RptSide = Amt->NextSiblingElement();
TiXmlElement* Pty = RptSide->FirstChildElement();

如果序列中缺少上述任何节点,则程序将因分段错误而中止。

有人可以帮忙吗?

谢谢

I am using tinyXml for parsing an XML file in C++. Could anyone please tell me how do I check to see if a node (parent/child/next sibling) exists or not. Below are the only nodes present in the xml file I'm working on.

TiXmlElement* Instrmt = TrdCaptRpt->FirstChildElement();
TiXmlElement* Undly = Instrmt->NextSiblingElement();
TiXmlElement* Amt = Undly->NextSiblingElement();
TiXmlElement* RptSide = Amt->NextSiblingElement();
TiXmlElement* Pty = RptSide->FirstChildElement();

if any of the nodes above is missing in the sequence then the program aborts with a segmentation fault.

Could anyone please help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2024-11-07 13:25:15

您可以使用 TiXmlNode 类的 const TiXmlNode* TiXmlNode::FirstChild ( const char * value ) const 函数并检查生成的 TiXmlNode* 是否为 NULL。

TiXmlNode* child = mynode->FirstChild();

if (child != NULL)
{
  //A child exists....
}

对于 Parent 你有类似的功能。您可以在此处找到文档。

我希望这有帮助。

You can use the const TiXmlNode* TiXmlNode::FirstChild ( const char * value ) const function of the TiXmlNode class and check if the resulting TiXmlNode* is NULL or not.

TiXmlNode* child = mynode->FirstChild();

if (child != NULL)
{
  //A child exists....
}

For Parent you have a similar function. You can find the documentation here.

I hope this helps.

夜深人未静 2024-11-07 13:25:15

TiXml 提供了 TiXMlHandle 类来负责检查 NULL,因此它应该排序排除分段错误。您仍然需要检查链末端的节点是否存在。

TiXml provides the TiXMlHandle class to take care of checking for NULL, so it should sort out the segmentaiton faults. You still need to check the existence of the node at the end of the chain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文