简体   繁体   中英

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

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.

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 (ie 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.

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

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.

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

If your XML document follows an XSD schema, you can use gSOAP to generate C/C++ struct s 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.)

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.

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.

If you have an XSD that describes your XML types there are products which can generate C++ classes which you can use to read in your document and then have an API to access and modify the document. One of the best comercial products out there that can do this is HydraExpress by Rogue Wave Software . This kind of software will generate the C++ code that you can compile and link in with your program. It is often going to have better performance than generated interfaces in languages that support reflection and dynamic object creation.

A disclaimer, I'm a developer of the HydraExpress software. If you need a free solution there are other options out there that may fit your needs and I recommend you try a few to find what's a best fit. In my bias opinion, HydraExpress is a good choice if you need performance, commercial tier support, and an easier to use solution than similar free products. It does cost money, but there is a trial that can be downloaded from the website.

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.

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