正在分析XML中C/C++头文件中定义的嵌套结构/联合

Parsing Nested Structures/Unions defined in C/C++ header files in XML

本文关键字:定义 文件 嵌套 联合 结构 C++ XML      更新时间:2023-10-16

我有一个如下所示的结构:(示例)

struct struct3
{
   struct structchild4
   {  
      float child5;
   } child6;
   unsigned int child7;
};

我希望这在XML中表示如下:

<tag1= "struct3">
        <name>struct3</name>
        <input_type>byte</input_type>
        <method></method>
        <tag_ref = "structchild4">
            <name>child6</name>
        </tag_ref>
        <tag2= "child7">
            <name>child7</name>
            <len>4</len>
            <value> </value>
        </tag2>
    </tag1>

我所遵循的方法是将其转换为gccXML格式,然后使用Visual C++对其进行解析。我使用xerces-c DOM解析器。

有人能建议怎么做吗?谢谢

更好的方法是反射,BoostLib有一些现成的。你做的事情像:

for( Attribute::Iterator it = reflectiveObject.getAttributeList().begin();
     it != reflectiveObject.getAttributeList().end();
     ++it )
{
    XML.createNode( it.getAttributeName() );
}

//那么方法也是如此。应该有一个递归遍历类型的上层迭代器,如果类型有一个子类或子结构,那么识别XML并为它们运行相同的代码。

没有反射会更无聊,你应该为它创建一个格式化程序和一个解析器,比如

if( dynamic_cast< DesirecClass* >( obj ) != NULL ){
    XML.createNode( typeid( obj ).name() );
}
// Hard Code (terrible treatment) for each attribute, etc...

还有一些可以搜索的解映射方法。