如何从ptree异常获取xml行号

时间:2022-08-23 20:45:08

I am using boost ptree to read an xml file like this:

我使用boost ptree来读取这样的xml文件:

ptree myTree;
... /*open xml file*/
try{
    myTree.get<string>(s);
}
catch(boost::exception const&  ex)
{
/*get useful info!*/
}

I know I can use the what() function, but it produces an error and the strings I just sent.

我知道我可以使用what()函数,但它会产生一个错误和我刚才发送的字符串。

Is there a way to get more useful information like the line numbers in the xml that are relevant to the call?

是否有一种方法可以获得更有用的信息,比如与调用相关的xml中的行号?

2 个解决方案

#1


2  

If you want to detect malformed XML (as opposed to XML documents which simply don't contain the values you expect, in which case line numbers aren't feasible to obtain):

如果您希望检测格式不正确的XML(与XML文档相反,XML文档不包含您期望的值,在这种情况下,无法获得行号):

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

int main(int argc, char* argv[])
{
  boost::property_tree::ptree pt;
  try {
    read_xml(argv[1], pt);
  } catch (const boost::property_tree::xml_parser::xml_parser_error& ex) {
    std::cerr << "error in file " << ex.filename() << " line " << ex.line() << std::endl;
  }
}

Now given that t.xml is not a valid XML document:

现在考虑到t。xml不是有效的xml文档:

$ a.out t.xml
error in file t.xml at line 10

#2


1  

A boost::property_tree has no concept of line numbers anymore. Basically it is just an iterable tree. It doesn't know if its contents where parsed from a file, added programmatically or came out of nowhere. Therefore there is just no way to get a line number when the tree doesn't contain the values you are looking for.

boost::property_tree不再具有行号的概念。基本上它只是一个可迭代的树。它不知道它的内容是从文件中解析出来的,是通过编程添加的,还是不知从哪里冒出来的。因此,当树不包含要查找的值时,就没有办法获得行号。

Things you might want to consider:

你可能需要考虑的事情:

  • Improve your XML schema to catch missing information on parse time. As @JohnZwinck already pointed out line numbers still exist while parsing. You should definitely be able to rule out "that the person creating the xml decided to change [anything structurally]" like this.
    You make it sound like they are in charge of deciding how the XML must look like. Even if this is the case your program still expects the XML to be formed in a certain way to do meaningful things with it. And this is where your schema comes into play. Now if they decide to change their schema you will instantly notice where there is a mismatch to the schema you designed for.
  • 改进您的XML模式,以便在解析时捕获丢失的信息。正如@JohnZwinck已经指出的那样,解析时仍然存在行号。您应该能够排除“创建xml的人决定(在结构上)改变任何东西”的可能性。您让它听起来像是他们负责决定XML的外观。即使是这种情况,您的程序仍然期望XML以某种方式形成,以便对其进行有意义的处理。这就是你的模式发挥作用的地方。现在,如果他们决定更改他们的模式,您将立即注意到哪里与您设计的模式不匹配。
  • Use another variant of get<string>. There are many variant allowing you to specify default values, getting null, or do something else, if the data you are expecting does not exist.
    Your try-instant-catch-debug-continue code pattern suggests that you aren't entirely sure what data to expect and that it is noncritical if the data isn't there. Exceptions are for exceptional situations. It seemsmto me that this isn't one.
  • 使用get 的另一种变体。如果您期望的数据不存在,那么有许多变体允许您指定默认值、获取null或执行其他操作。您的try- instance -catch-debug-continue代码模式表明,您并不完全确定需要得到什么数据,如果数据不存在,那么它是非关键的。例外是在特殊情况下。我觉得这不是一个。

#1


2  

If you want to detect malformed XML (as opposed to XML documents which simply don't contain the values you expect, in which case line numbers aren't feasible to obtain):

如果您希望检测格式不正确的XML(与XML文档相反,XML文档不包含您期望的值,在这种情况下,无法获得行号):

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

int main(int argc, char* argv[])
{
  boost::property_tree::ptree pt;
  try {
    read_xml(argv[1], pt);
  } catch (const boost::property_tree::xml_parser::xml_parser_error& ex) {
    std::cerr << "error in file " << ex.filename() << " line " << ex.line() << std::endl;
  }
}

Now given that t.xml is not a valid XML document:

现在考虑到t。xml不是有效的xml文档:

$ a.out t.xml
error in file t.xml at line 10

#2


1  

A boost::property_tree has no concept of line numbers anymore. Basically it is just an iterable tree. It doesn't know if its contents where parsed from a file, added programmatically or came out of nowhere. Therefore there is just no way to get a line number when the tree doesn't contain the values you are looking for.

boost::property_tree不再具有行号的概念。基本上它只是一个可迭代的树。它不知道它的内容是从文件中解析出来的,是通过编程添加的,还是不知从哪里冒出来的。因此,当树不包含要查找的值时,就没有办法获得行号。

Things you might want to consider:

你可能需要考虑的事情:

  • Improve your XML schema to catch missing information on parse time. As @JohnZwinck already pointed out line numbers still exist while parsing. You should definitely be able to rule out "that the person creating the xml decided to change [anything structurally]" like this.
    You make it sound like they are in charge of deciding how the XML must look like. Even if this is the case your program still expects the XML to be formed in a certain way to do meaningful things with it. And this is where your schema comes into play. Now if they decide to change their schema you will instantly notice where there is a mismatch to the schema you designed for.
  • 改进您的XML模式,以便在解析时捕获丢失的信息。正如@JohnZwinck已经指出的那样,解析时仍然存在行号。您应该能够排除“创建xml的人决定(在结构上)改变任何东西”的可能性。您让它听起来像是他们负责决定XML的外观。即使是这种情况,您的程序仍然期望XML以某种方式形成,以便对其进行有意义的处理。这就是你的模式发挥作用的地方。现在,如果他们决定更改他们的模式,您将立即注意到哪里与您设计的模式不匹配。
  • Use another variant of get<string>. There are many variant allowing you to specify default values, getting null, or do something else, if the data you are expecting does not exist.
    Your try-instant-catch-debug-continue code pattern suggests that you aren't entirely sure what data to expect and that it is noncritical if the data isn't there. Exceptions are for exceptional situations. It seemsmto me that this isn't one.
  • 使用get 的另一种变体。如果您期望的数据不存在,那么有许多变体允许您指定默认值、获取null或执行其他操作。您的try- instance -catch-debug-continue代码模式表明,您并不完全确定需要得到什么数据,如果数据不存在,那么它是非关键的。例外是在特殊情况下。我觉得这不是一个。