简体   繁体   English

RapidXML节点异常处理

[英]RapidXML node exception handling

I'm using RapidXML to parse a xml file containing some variables that I'd like to use in my C++ program. 我正在使用RapidXML解析一个xml文件,其中包含一些我想在我的C ++程序中使用的变量。 I am abled to read valid nodes but I'd like to add some error handling if a node name is misspelled for example. 我能够读取有效的节点,但是例如,如果节点名称拼写错误,我想添加一些错误处理。

Here is a working example of my files. 这是我的文件的工作示例。

.cpp 的.cpp

try
{
    rapidxml::file<> xmlFile("file.xml");
    rapidxml::xml_document<> doc;
    doc.parse<parse_declaration_node | parse_no_data_nodes>(xmlFile.data());

    xml_node<>* prop_node = doc.first_node("PropertyList");
}
catch (const runtime_error& e)
{
    // Do something
}
catch (const rapidxml::parse_error& e)
{
    // Do something
}
catch (const exception& e)
{
    // Do something
}
catch(...)
{
    // Do something
}

.xml .XML

<?xml version="1.0" encoding="utf-8"?>

<PropertyList>
    ...
</PropertyList>

If i however change the first_node() call in my .cpp file to a node that doesn't exists, like so: 但是,如果我将.cpp文件中的first_node()调用更改为不存在的节点,例如:

xml_node<>* prop_node = doc.first_node("Property");

The program crashed in run time complaining about bad memory access instead of throwing an exception. 该程序在运行时崩溃,抱怨内存访问错误,而不是引发异常。

Is this the way RapidXML is supposed to work or am I doing something wrong? 这是RapidXML应该工作的方式还是我做错了什么?

Thanks! 谢谢!

I'm not entirely familiar with RapidXML, but I found from its reference page the following about first_node. 我对RapidXML并不完全熟悉,但是我从它的参考页中发现了有关first_node的以下内容。 http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1what_is_rapidxml http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1what_is_rapidxml

function xml_node::first_node 函数xml_node :: first_node

Synopsis 概要

xml_node* first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; xml_node * first_node(const Ch * name = 0,std :: size_t name_size = 0,bool case_sensitive = true)const; Description 描述

Gets first child node, optionally matching node name. 获取第一个子节点,可以选择匹配节点名称。 Parameters 参数

name Name of child to find, or 0 to return first child regardless of its name; name要查找的子项的名称,或0而不管其第一个子项的名称; this string doesn't have to be zero-terminated if name_size is non-zero name_size Size of name, in characters, or 0 to have size calculated automatically from string case_sensitive Should name comparison be case-sensitive; 如果name_size不为零,则此字符串不必以零结尾。name_size名称的大小(以字符为单位),或者为0即可从字符串case_sensitive自动计算大小。名称比较应该区分大小写; non case-sensitive comparison works properly only for ASCII characters Returns 不区分大小写的比较仅适用于ASCII字符

Pointer to found child, or 0 if not found. 指向找到的孩子的指针;如果找不到,则为0。

So you can either call first_node without having to specifying its name.(Although this might not be a direct answer to your problem.) 因此,您可以调用first_node而不需要指定其名称。(尽管这可能不是您问题的直接答案。)

Or check if the return value of doc.first_node("Property") is 0. And only assign the value to prop_node if the return value is not 0. 或检查doc.first_node(“ Property”)的返回值是否为0。如果返回值不为0,则仅将值分配给prop_node。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM