Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an XML node to a C++ Structure?

Tags:

c++

xml

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.

like image 789
Bizwoo Avatar asked Sep 16 '10 12:09

Bizwoo


People also ask

Can XML elements be empty?

Empty XML Elements An element with no content is said to be empty. The two forms produce identical results in XML software (Readers, Parsers, Browsers). Empty elements can have attributes.

What is XML Element?

XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these. Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.

What are the parts of an XML file called?

An XML document consists of three parts, in the order given: An XML declaration (which is technically optional, but recommended in most normal cases) A document type declaration that refers to a DTD (which is optional, but required if you want validation) A body or document instance (which is required)


5 Answers

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.

like image 75
Martin Ba Avatar answered Oct 02 '22 22:10

Martin Ba


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

like image 24
monoceres Avatar answered Oct 03 '22 00:10

monoceres


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

like image 40
Etienne de Martel Avatar answered Oct 03 '22 00:10

Etienne de Martel


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.

like image 45
Björn Pollex Avatar answered Oct 02 '22 22:10

Björn Pollex


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.)

like image 23
Bruno Avatar answered Oct 03 '22 00:10

Bruno