简体   繁体   English

如何编写通用C ++函数将xml解析为结构?

[英]How do I write a generic C++ function to parse xml into structs?

I have a collection of xml data files representing objects of interest in images (rectangles, points, labelled faces, etc.) that I want to parse to produce vectors of structs. 我有一个xml数据文件集合,这些文件代表要解析的图像(矩形,点,带标签的面孔等)中感兴趣的对象,以生成结构向量。 The files are manually created (and so are not just the result of serialising some C++ objects) and are of the following form: 这些文件是手动创建的(因此不仅仅是序列化某些C ++对象的结果),其格式如下:

<root>
<image filename=whatever>
<object>
  <x>1</x>
  <y>2</y>
</object>
<object>
  <x>3</x>
  <y>4</y>
</object>
</image>
<image filename=something>
 ...
</image>
</root>

So a collection of images, each of which contains a collection of object children, each of which has children giving the data relevant to that object. 因此,一个图像集合,每个图像都包含一个对象子对象集合,每个子对象都具有提供与该对象相关的数据的子对象。 The structure of this data varies between files, eg in one file each object might just have an x and ay and in another each object might contain ints x1, y1, x2, y2 and a double z. 这些数据的结构在文件之间有所不同,例如,在一个文件中,每个对象可能只具有x和ay,在另一个文件中,每个对象可能包含int x1,y1,x2,y2和双精度z。

I want to parse such a file to produce a vector of Objects, where Object is a struct, in this case of the form struct Object { int x; 我想解析这样的文件以生成对象的向量,其中对象是一个结构,在这种情况下为结构struct Object {int x; int y; 诠释 }. }。

For different choices of Object, I've currently got separate functions that use rapidxml to parse the xml in identical ways, except for which fields they extract. 对于Object的不同选择,我目前拥有单独的函数,这些函数使用rapidxml以相同的方式解析xml,但它们提取的字段不同。

I'd like to write a templated function so that you can merely specify the elements of a struct in some way and have the function return a vector of the appropriate structs. 我想编写一个模板化函数,以便您只能以某种方式指定结构的元素,并使该函数返回适当结构的向量。 ie The user should specify a list of pairs ("x1", int), ("x2", int), etc. and have the rest of the work be done automatically. 即,用户应指定对(“ x1”,int),(“ x2”,int)等对的列表,并使其余工作自动完成。

I'm sure there must be a nice boost solution to this problem that avoids having to use XML schema. 我确信必须有一个很好的解决方案来避免使用XML模式。 How do I do this? 我该怎么做呢?

You could try Boost Property Tree . 您可以尝试Boost属性树

It allows you to write your own load/save functions to map XML (or INI or JSON) data onto your own structures. 它允许您编写自己的加载/保存函数,以将XML(或INI或JSON)数据映射到自己的结构上。 See the tutorial . 请参阅教程

It even uses RapidXML which you're already using. 它甚至使用您已经在使用的RapidXML。

Edit: 编辑:

You could try something like 您可以尝试类似

template<typename T>
struct Field
{
    typedef T type;
    std::string name;
};

template<typename... Fields>
std::tuple<typename Fields::type...>
load(const Data& data, Fields... f)
{
    return std::make_tuple( data.get<typename Fields::type>(f.name)... );
}

Where Data is some source of the input data, like a boost::ptree , and you'd use that function like this: 其中Data是输入数据的某种来源,例如boost::ptree ,您将使用以下函数:

load(d, Field<int>{"x1"}, Field<int>{"x2"} );

Then you'd just need each data type to be constructible from a tuple of the right types. 然后,您只需要从正确类型的元组中构造每种数据类型即可。

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

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