简体   繁体   中英

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

I have a structure represented as follows: (Example)

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

I want this to be represented as follows in 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>

The method I'm following is that I'm converting this into a gccXML format and I then parse it using Visual C++. I use the xerces-c DOM parser.

Could anyone suggest how to go about doing this? Thanks!

The better way to do this is reflection, BoostLib has some ready to use. You do something like:

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

//then the same for methods. Should have an upper iterator going recursively through types, if type has a sub-class or sub-structure, then ident the XML and run the same code for them.

Without reflection is pretty more boring, you should create and Formater and a Parser for it, like

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

There's also some demangling methods that you can search for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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