使用 Tinyxml 检查 XML 节点是否存在
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
TiXmlNode
类的const TiXmlNode* TiXmlNode::FirstChild ( const char * value ) const
函数并检查生成的TiXmlNode*
是否为 NULL。对于 Parent 你有类似的功能。您可以在此处找到文档。
我希望这有帮助。
You can use the
const TiXmlNode* TiXmlNode::FirstChild ( const char * value ) const
function of theTiXmlNode
class and check if the resultingTiXmlNode*
isNULL
or not.For Parent you have a similar function. You can find the documentation here.
I hope this helps.
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.