Solved

converting C struct to XML tags .

Posted on 2003-10-29
8
454 Views
Last Modified: 2013-11-19
Frnds ,

I have below reqrmnt .

I need to convert a C struct into xml tags .
Ex

suppose if i have a struct

struct emp {

name char[10],
sal     float
};

I need to create a string like "<name>name from struct</name> <sal> sal from struct</sal>"  .

Here prob is my struct is dynamic . it may contain any number of elements in it .  So I need to know the struct element name and the datatype dynamically . Do you guys have any idia abt how to go ahead ? If its through C++ also its OK for me .

Pls give ur valuable suggestions .

Thanks in adv
Raghava.
0
Comment
Question by:raghava_dg
  • 3
  • 2
8 Comments
 
LVL 5

Expert Comment

by:drnick
ID: 9642072
you got a serious problem.
the c compiler removes every variable/type names and info.
you cannot determine a structs elements at runtime.
so you cannot do what you wanted.
if you want to do it like that, you need another dynamic list, which contains the type and name (and maybe adress-offset) information for your struct.
like  
 struct {
  char *name;
  int datatype;
  int addr_ofs;
} info;

#define DATATYPE_CHAR 0
#define DATATYPE_INT 1
#define DATATYPE_BOOL 2
...

then a list of this will do, but you'll need a complicated decoding routine.

another way would be the usage of classes.
you could do one base class with the virtual method to_xml_string which returns a string.
then you could derive classes for every structure you need.
the derived classes could contain the data fields as members you require and you would
let them have an individual to_xml_string routine each.
this would be way more easy to programm, but it is not dynamical anymore, since you gotta
know all possible structures at compile time.

if you need additional hints to any of these suggestions, let me know.


0
 

Author Comment

by:raghava_dg
ID: 9642154
thanks drnick ,

yes C approch is complicated and in C++ i can't dynamically convert .
as u said looks like  I got into serious trouble .....:)) . any other suggestions welcome . :))
0
 
LVL 5

Expert Comment

by:drnick
ID: 9647912
hm, what is the main thing you want to achieve?
maybe we can find a way around the dynamic-struct-thing
0
 

Author Comment

by:raghava_dg
ID: 9655514
I want to write a utility which will accept a C structure and convert it to xml file as explained above.If same can be achived in c++ that should also be fine.
0
 
LVL 5

Accepted Solution

by:
drnick earned 50 total points
ID: 9655687
i'm sorry, but that is not really possible.
structs exist in the ram as bulk of memory, and memory doesn't contain type information.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now