tags:

views:

136

answers:

8

Hi, I am new in programming and I would like to know if it is possible to convert an XML node to a C++ Structure. For instance I have a file with the following XML:

<?xml version="1.0" encoding="utf-8"?>
<StrucDescription>
  <StrucName>
    <unStrucNameMember1 type="uint16">0</unStrucNameMember1>
    <unStrucNameMember2 type="uint8">0</unStrucNameMember2>
    <ulStrucNameMember3 type="int32">0</ulStrucNameMember3>
    <bStrucNameMember4 type="bool">true</bStrucNameMember4>
    <szStrucNameMember5 type="char" size="32"></szStrucNameMember5>
  </StrucName>
</StrucDescription>

Is it possible to create the bellow structure for future data storing from the above XML?

struct StrucName
{
  uint16  unStrucNameMember1;
  uint8   unStrucNameMember2;
  int32   ulStrucNameMember3;
  bool    bStrucNameMember4;
  char   szStrucNameMember5[32];

  StrucName ()
  : unStrucNameMember1(0)
  , unStrucNameMember2(0)
  , ulStrucNameMember3(0)
  , bStrucNameMember4(true)
  , szStrucNameMember5()
};

I thank you all for the answers.

+1  A: 

If the type of the struct is know at compile time, you can use an XML-parser. There is no way in C++ to dynamically create a type (struct in your case) at runtime. If you want to generate code for later working with an XML-Schema, this may be what you are looking for.

Space_C0wb0y
+2  A: 

This is the job of XML Parsers. A good xml parser that is easy to set up is Tiny XML.

monoceres
+2  A: 

C++ has no native XML parsing utilities. You'll have to get an external library, such as Xerces, for that.

Etienne de Martel
A: 

Yes, it's possible. I suggest you retag your question for ruby, python, and/or perl, as they'll all have an XML parsing library suitable for reading your XML definition, and it will probably be easier to get started quickly than the higher-performance but typically heavyweight C++ equivalents. You loop over the structure definitions and use an internal loop for the fields, printing out the C++ code you'd like to generate. If you do want to use C++, you could try libxml2 or xerces.

Tony
Never popular with C++ programmers hey :-).
Tony
A: 

If your XML document follows an XSD schema, you can use gSOAP to generate C/C++ structs from the XSD and convert between these structures and XML documents. (Although this tool is intended for use with SOAP, this can be done independently of using SOAP.)

Bruno
The "mixed C/C++" generation does frighten me a bit. There is no C/C++ language, so what does it generate ?
Matthieu M.
@Matthieu M., sorry for the ambiguity. It depends on the flag you use, with `-c` the tools generate C, otherwise C++ (more details in the man pages for `wsdl2h` and `soapcpp2`).
Bruno
@Bruno: Thanks, when I tried the online generation I only saw C and wondered if they simply made the code both C and C++ compliant... which is possible but makes for a terrible interface C++ wise.
Matthieu M.
A: 

AFAIK, You cannot, with C++, create a structure dynamically.

You will need to define your structure according to the format of the XML file; normally such things are done at design time.

M.

Max
+1  A: 

Creating programming language constructs for XML documents is called XML data binding. If you want to do this at (before) compile-time, then google for C++ xml data binding. The most promising tool I have seen so far is XSD from Codesynthesis. It's available as GPLed version. (NOte that you need an XML schema describing your file

If you want to do this at run time (dynamically) for arbitrary XML structures -- this is not possible. Since you write that you are "new to programming" I suggest starting with a C++ beginners book and it will then become apparent pretty quickly why it is not possible: You (or a tool) write the source code for the struct and its usage. To reference your StrucName by this moniker, you have to know at the time you write the code (i.e. at compile time) that you have an XML tag by this name. If you only knew the XML layout and its name at runtime, you cannot refer to these monikers in your sourcecode as they are not known yet.

Martin
A: 

What you are asking for dynamically generating the structures is impossible.

In C++ the structure should be known at compilation-time. There are code generation tools that can read XSD files and generate the structures for you. You can then integrate then in your code.

Such code generation is common, and generally is part of the build process so that whenever the description of the schema changes the structures are regenerated.

If you want to parse arbitrary xml however, you cannot have such an easy-binding because C++ structures / classes do not evolve at runtime.

You can however have generic classes, binding the data into fields, that is what DOM parsers essentially feature:

  • TinyXml
  • Xerces
  • ...

Finally, if you want structures which are dynamically generated from xml files, you'll need dynamic languages such as Python or Ruby, which support adding / removing attributes of an object at runtime. If performance isn't an issue, this might prove much easier.

Furthermore, the interpreters are generally shipped with a good quantities of well-documented libraries, among which Xml and Json parsers figure.

Matthieu M.