C++ 解析C/C+中定义的嵌套结构/联合+;XML中的头文件
我有一个如下所示的结构:(示例) 我希望用XML表示如下:C++ 解析C/C+中定义的嵌套结构/联合+;XML中的头文件,c++,c,visual-c++,C++,C,Visual C++,我有一个如下所示的结构:(示例) 我希望用XML表示如下: <tag1= "struct3"> <name>struct3</name> <input_type>byte</input_type> <method></method> <tag_ref = "structchild4"> <name>ch
<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>
结构3
字节
孩子6
孩子7
4.
<>我所遵循的方法是把它转换成GCXML格式,然后用Visual C++解析它。我使用xerces-CDOM解析器
有人能建议如何着手做这件事吗?谢谢 更好的方法是反射,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...
if(动态_cast(obj)!=NULL){
createNode(typeid(obj.name());
}
//每个属性的硬代码(糟糕的处理)等。。。
您还可以搜索一些非常有用的方法。谢谢@Rodrigo Gurgel。我的代码接受gccXML创建的XML,并以其他特定格式返回XML。我90%的代码使用xerces-c。所以,我也不得不用Xerces-C来做这件事,因为一些未知的原因,这给了我一段艰难的时间。
if( dynamic_cast< DesirecClass* >( obj ) != NULL ){
XML.createNode( typeid( obj ).name() );
}
// Hard Code (terrible treatment) for each attribute, etc...